Reputation: 2155
I've created a web page with textbox control as below -
<asp:TextBox ID="txtNumber" runat="server" size="5" MaxLength="9" AutoPostBack="True"
OnTextChanged="txtNumber_TextChanged" CssClass="txtBoxPage" Style="margin-left: 3px;"></asp:TextBox>
I'm facing an issue when pressing enter key in textbox. On Enter key press postback is not occured. Can you please provide me help so that on Enter Key Press, page should get postback and textbox server-side will get fire.
I've debug the code and found that the WebForm_TextBoxKeyHandler returns false when the ENTER key is pressed. Below is the rendered code for reference -
<input type="text" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" onchange="javascript:setTimeout('__doPostBack(\'textbox1\',\'\')', 0)" ... />
Thanks in advance.
Upvotes: 1
Views: 7846
Reputation: 8511
Try following workaround -
//Page_Load method
txtNumber.Attributes.Add("onkeyup", "OnKeyUp(event);");
//JavaScript code
function OnKeyUp(event) {
var key = (event.keyCode) ? event.keyCode : event.which;
if (key != null) {
key = parseInt(key, 10);
if (key == 13) {
document.body.setActive();
document.body.focus();
window.event.srcElement.focus();
}
}
}
Above code will fire the PostBack event as focus is being lost and regained from the textbox.
Upvotes: 3