javascript - On Enter Key Press on textbox server-side postback event not getting fired - Stack Overflow

I've created a web page with textbox control as below -<asp:TextBox ID="txtNumber" ru

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.

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.

Share Improve this question asked Oct 3, 2012 at 12:39 Sumit DeshpandeSumit Deshpande 2,1552 gold badges21 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745634760a4637323.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信