I have come into problems regarding adding the Entery Key functionality on the textboxes. I have found a solution and wanted to share it too.
Example 1: this example has 4 HTML input type textboxes. every time you press enter on a textbox, the focus (cursor) will transfer to the other textbox.
Example 3: this example has 1 ASP textbox and 1 ASP button. when you type something in the textbox and PRESS ENTER, the click event on the button would be triggered.
Many of these issues can be avoided by dealing with form submissions rather than button clicks:
<asp:Button
ID="btnMySubmitButton"
UseSubmitBehavior="true"
runat="server"
Text="Click or press Enter"
/>
Note: no click event handlers for that button. We do, however, use a true value for UseSubmitBehavior (default is TRUE)
In your code-behind, check to see if the page was submitted (rather than initial load) and then react to that.protected
void Page_Load(object sender,
EventArgs e)
{
if (IsPostBack)
{
// enter key was pressed
// do something here rather than using the button click event
}
}
My two-cents.
Please click 'Mark as Answer' if reply assisted you.
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams
UseSubmitBehavior gets called only when there is focus to any controls in the page. Is there any option so that when i first load the page and click on the enter should call my button's clcik event
Member
88 Points
54 Posts
Solution: Enter Key on Textboxes
Dec 19, 2007 08:49 PM|drchanix|LINK
I have come into problems regarding adding the Entery Key functionality on the textboxes. I have found a solution and wanted to share it too.
Example 1: this example has 4 HTML input type textboxes. every time you press enter on a textbox, the focus (cursor) will transfer to the other textbox.
<input id="Text2" type="text" onKeyDown="if(event.keyCode==13) event.keyCode=9;"/>
<input id="Text3" type="text" onKeyDown="if(event.keyCode==13) event.keyCode=9;"/>
<input id="Text4" type="text" onKeyDown="if(event.keyCode==13) event.keyCode=9;"/>
<input id="Text5" type="text" onKeyDown="if(event.keyCode==13) event.keyCode=9;"/>
Example 2: this example has 4 ASP type textboxes. every time you press enter on a textbox, the focus (cursor) will transfer to the other textbox.
<asp:TextBox ID="TextBox2" runat="server" onKeyDown="if(event.keyCode==13) event.keyCode=9;"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server" onKeyDown="if(event.keyCode==13) event.keyCode=9;"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server" onKeyDown="if(event.keyCode==13) event.keyCode=9;"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server" onKeyDown="if(event.keyCode==13) event.keyCode=9;"></asp:TextBox>
Example 3: this example has 1 ASP textbox and 1 ASP button. when you type something in the textbox and PRESS ENTER, the click event on the button would be triggered.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" /><br />
(code behind)
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("test")
End Sub
Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + Button1.UniqueID + "').click();return false;}} else {return true}; ")
End Sub
Hope this helps.
Contributor
5194 Points
1339 Posts
Re: Solution: Enter Key on Textboxes
Dec 20, 2007 03:45 PM|pixelsyndicate|LINK
Many of these issues can be avoided by dealing with form submissions rather than button clicks:
<asp:Button ID="btnMySubmitButton" UseSubmitBehavior="true" runat="server" Text="Click or press Enter" /> Note: no click event handlers for that button. We do, however, use a true value for UseSubmitBehavior (default is TRUE) In your code-behind, check to see if the page was submitted (rather than initial load) and then react to that.protected void Page_Load(object sender, EventArgs e){
if (IsPostBack){
// enter key was pressed // do something here rather than using the button click event}
}
My two-cents.
"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams
Member
240 Points
125 Posts
Re: Solution: Enter Key on Textboxes
Jan 07, 2008 06:43 AM|Shobana P.S|LINK
Hi,
UseSubmitBehavior gets called only when there is focus to any controls in the page. Is there any option so that when i first load the page and click on the enter should call my button's clcik event
Thanks,
Shobana
Shobana