|
| Solution: Enter Key on Textboxes |
 |
Thu, 20 Dec 2007 01:49:30 +000 |
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.
|
| Post Reply
|
| Re: Solution: Enter Key on Textboxes |
 |
Thu, 20 Dec 2007 20:45:45 +000 |
Many of these issues can be avoided by dealing with form submissions rather than
button
clicks:<asp:ButtonID="btnMySubmitButton"UseSubmitBehavior="tru
e"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.protectedvoid 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.
|
| Post Reply
|
| Re: Solution: Enter Key on Textboxes |
 |
Mon, 7 Jan 2008 11:43:51 +0000 |
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
|
| Post Reply
|
|
|
|
|
|
|
|
|
|