简体   繁体   English

客户端按键处理事件,设置焦点功能和__doPostBack ASP.NET

[英]Client side keypress handling event, set focus function, and __doPostBack ASP.NET

I'm trying to do the following : 我正在尝试执行以下操作:

  • First, a textBox get the focus on page load (no problem here) 首先,文本框将重点放在页面加载上(这里没有问题)

  • Then, when ENTER button is pressed inside that textBox, it switch focus to the second textBox. 然后,当在该文本框内按下ENTER按钮时,它将焦点切换到第二个文本框。

  • Finally, when ENTER is pressed in that second textBox, it does a postback and reahes code behind event just like when you press a classic <asp:button /> 最后,当在第二个文本框中按下ENTER时,它会进行回发并在事件后重新捕获代码,就像您按下经典的<asp:button />

Based on : 基于 :

Fire event on enter key press for a textbox 按下Enter键可触发文本框的火灾事件

and

JavaScript set focus to HTML form element JavaScript将焦点设置为HTML表单元素

I managed to come up with : 我设法提出:

HTML : HTML:

 <asp:TextBox ID="txtNNoSerie" runat="server"  Width="150px" ClientIDMode="Static" onkeypress="EnterEventFirst(event)" AutoPostBack="false"></asp:TextBox>             
 <asp:TextBox ID="txtNNoIdentification" runat="server" Width="150px" ClientIDMode="Static" onkeypress="EnterEventSecond(event)" AutoPostBack="false"></asp:TextBox>
 <asp:Button id="btnAjouter" CssClass="ms-Button ms-Button--primary" onclick="btnAjouter_click" Text="+" ForeColor="White" runat="server"  Width="30px" />

    <input type="text" id="txtTest" /> <%-- Just for tests --%>

JS : JS:

  function EnterEventFirst(e) {
      if (e.keyCode == 13) {
          document.getElementById('txtTest').value = 'got it';
          document.getElementById('<%=txtNNoIdentification.ClientID%>').focus();
      }
  } 



  function EnterEventSecond(e) {
      if (e.keyCode == 13) {
          document.getElementById('txtTest').value = 'Again';
          __doPostBack('<%=btnAjouter.ClientID%>', "");
      }
  }

Code behind : 后面的代码:

    protected void btnAjouter_click(object sender, EventArgs e)
    {
    }

JavaScript functions are reached, because i can see "got it" and "Again" in the test TextBox, but just for half a second and then it desapear... The __DoPostBack funtion does not work. 达到了JavaScript函数,因为我可以在测试TextBox中看到“ get it”和“ Again”,但是只等待了半秒钟,然后消失了……__DoPostBack函数不起作用。

Looks like when you press enter in a textBox, it automaticaly does a useless postback and page reload... And that is where i'm stuck 看起来当您在textBox中按Enter时,它会自动执行无用的回发和页面重新加载...这就是我遇到的问题

使用ClientID而不是UniqueID。

Well, textBox automaticaly postBack when enter is pressed, ok then. 好吧,当按下enter键时,textBox会自动postBack,然后确定。

I did it all on server side, I hate this but i kinda had no choice : 我在服务器端完成了所有操作,我讨厌这个,但是我别无选择:

        txtNNoSerie.Focus();
        if (txtNNoSerie.Text != "")
            txtNNoIdentification.Focus();
        if (txtNNoSerie.Text != "" && txtNNoIdentification.Text != "")
            btnAjouter_click(this, new EventArgs());

Server side code... If anyone can show me some working client side code i'll take it... 服务器端代码...如果有人可以向我展示一些有效的客户端代码,我会接受...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM