Fire event on enter key press for a textbox
Wrap the textbox inside
asp:Panel
tagsHide a Button that has a click event that does what you want done and give the
<asp:panel>
aDefaultButton
Attribute with the ID of the Hidden Button.
<asp:Panel runat="server" DefaultButton="Button1"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" style="display:none" OnClick="Button1_Click" /></asp:Panel>
ASPX:
<asp:TextBox ID="TextBox1" clientidmode="Static" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox> <asp:Button ID="Button1" runat="server" style="display:none" Text="Button" />
JS:
function EnterEvent(e) { if (e.keyCode == 13) { __doPostBack('<%=Button1.UniqueID%>', ""); } }
CS:
protected void Button1_Click1(object sender, EventArgs e) { }
You could wrap the textbox and button in an ASP:Panel, and set the DefaultButton property of the Panel to the Id of your Submit button.
<asp:Panel ID="Panel1" runat="server" DefaultButton="SubmitButton"> <asp:TextBox ID="TextBox1" runat="server" /> <asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click" /></asp:Panel>
Now anytime the focus is within the Panel, the 'SubmitButton_Click' event will fire when enter is pressed.