最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far