最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论