I can set a page button to be the default button (e.g. when enter key pressed) in code-behind. Is it possible to do the same in javascript? I want to use OnClientClick to do this without need for a postback. Thanks.
You can use Jquery to set the default button like below
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script>
$(document).ready(function () {
//Capture the enter button click on form
$(document).bind("keydown", function (event) {
//Get the key code
var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
//check if the click event is enter button click
if (keycode == 13) { // keycode for enter key
//Trigger the button click
document.getElementById('Button1').click();
}
});
});
</script>
I want to use OnClientClick to do this without need for a postback.
If you want to set the default button with out using code behind, Then you just need to set the default button property of form in html mark up itself like below
This solution will run every time the user hits the enter key. I only want the default to be set when I am showing a specific DIV element that becomes visible when a user clicks the first button. I have 2 different image buttons on the page that I need
to set a default button when that DIV shows. For example, if DIV id=div1 is showing then make Button1 the default and if DIV id=div2 is showing make Button2 the default. Both need to be done in js/jquery without postback. Also, the buttons are ImageButtons
if that makes any difference.
The ASP.NET Panel control will be rendered as <div> in browser, and it enable us specify
DefaultButton property to indicate which button gets clicked when the Panel control has focus and the user presses the ENTER key. You could refer to the following sample.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
351 Points
1535 Posts
Set button as default
Aug 04, 2016 07:57 PM|dlchase|LINK
I can set a page button to be the default button (e.g. when enter key pressed) in code-behind. Is it possible to do the same in javascript? I want to use OnClientClick to do this without need for a postback. Thanks.
All-Star
50831 Points
9895 Posts
Re: Set button as default
Aug 04, 2016 08:38 PM|A2H|LINK
You can use Jquery to set the default button like below
HTML
C#:
Aje
My Blog | Dotnet Funda
All-Star
50831 Points
9895 Posts
Re: Set button as default
Aug 04, 2016 08:41 PM|A2H|LINK
If you want to set the default button with out using code behind, Then you just need to set the default button property of form in html mark up itself like below
Aje
My Blog | Dotnet Funda
Member
351 Points
1535 Posts
Re: Set button as default
Aug 04, 2016 08:52 PM|dlchase|LINK
This solution will run every time the user hits the enter key. I only want the default to be set when I am showing a specific DIV element that becomes visible when a user clicks the first button. I have 2 different image buttons on the page that I need to set a default button when that DIV shows. For example, if DIV id=div1 is showing then make Button1 the default and if DIV id=div2 is showing make Button2 the default. Both need to be done in js/jquery without postback. Also, the buttons are ImageButtons if that makes any difference.
All-Star
40535 Points
6233 Posts
Microsoft
Re: Set button as default
Aug 05, 2016 02:18 AM|Fei Han - MSFT|LINK
Hi dlchase,
The ASP.NET Panel control will be rendered as <div> in browser, and it enable us specify DefaultButton property to indicate which button gets clicked when the Panel control has focus and the user presses the ENTER key. You could refer to the following sample.
Best Regards,
Fei Han