Challenges:
As I mentioned in my previous post, I fixed the issue of hitting enter key on an ASP.NET page with a single textbox and submit button. But now new issue occurred: I could not use the Enter key on other button web controls. Whenever I hit the Enter key in the web form, the onClick event of the search button fires not the other submit buttons. I know I am using Master page and the search button is included in every page based on this master page. And the search button is in the first button web control position.
Trouble Shootings:
OK, the first thing came to my mind to resolve the issue above was to use the new DefaultButton properties in ASP.NET 2.0 form or panel controls. But since I am using Masterpage, when I put defaultButton=”btnSubmit” in master page or put the following code in my Page_load event
Page.Form.DefaultButton = “btnSubmit”
I got the following error:
Server Error in ‘/MyApplication’ Application.
The DefaultButton of ‘form1′ must be the ID of a control of type IButtonControl.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The DefaultButton of ‘form1′ must be the ID of a control of type IButtonControl.
Solutions:
How to resolve this problem? The trick here is to use the controls UniqueID property that returns its long client name. So, you can use the following code in your page based on Master page:
Page.Form.DefaultButton = btnSubmit.UniqueID
This way, when you press Enter, the form will post back to the server and the code in the event handler for btnSubmit.Click will fire.
The same method can be applied to defaultFocus property, of course you need to use ClientID for the textbox you need to auto-focus on.
Remember:
1) The code sample above should be put in the page which is based on Master page, not the master page itself.
2), The sample code above is in VB.net, for C# you can use the following
Tags: asp.net, onClickthis.form.DefaultButton = this.yourbutton.UniqueID
Good Article..:)
keep it up good work
[Reply]
Thanks,Your solution works!!.
[Reply]
You can also wrap the form elements in an asp:panel and set the defaultButton property of the panel to the button name. For example.
stuff
[Reply]
Sorry about that, by brackets got escaped.
<asp:panel runat=”server” ID=”submit” defaultButton=”imgBtnSubmit”>
[Reply]
Excelent… it works!!! Thanks
[Reply]
this thing is working in firefox but not working in IE 8 so kindly suggest what to do?
[Reply]
it dosn’t work, if you have many UserControls on one page and you want to decide, witch contols is a default.
I haveanother solution:
body.Attributes.Add(”onkeypress”, String.Format(”return controlEnter(’{0}’,event,’{1}’)”, yourEnterButton, yourEscapeButton))
and JS-Function:
function controlEnter(obj,event,escape) {
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (keyCode == 13) {
if (document.getElementById(obj)!== null)
{
document.getElementById(obj).click();
}
return false;
}
if (keyCode == 27) {
if (document.getElementById(escape)!== null)
{
document.getElementById(escape).click();
}
return false;
}
return true;
}
[Reply]