1

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.

  • 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 />

Based on :

Fire event on enter key press for a textbox

and

JavaScript set focus to HTML form element

I managed to come up with :

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 :

  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.

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

Community
  • 1
  • 1
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62

2 Answers2

1

Use ClientID instead of UniqueID.

nAviD
  • 2,784
  • 1
  • 33
  • 54
  • Ok, now the focus work, but anyway the page Reload cancels everything – Antoine Pelletier Jan 29 '16 at 21:37
  • it focuses for half a second, then the focus is back on the first textBox – Antoine Pelletier Jan 29 '16 at 21:46
  • your problem is unclear ! you have only two textbox and no submit button. you press Enter in first textbox , and then in second textbox . when you press Enter in second one it runs __dopostback function which causes the form to be submited and page to be refreshed. what is the problem here ? – nAviD Jan 29 '16 at 22:05
  • there is a button, but when someone press enter in the second textbox, it's just as if he pressed that button. – Antoine Pelletier Feb 01 '16 at 14:53
  • that's because of your own code ( __doPostBack('<%=btnAjouter.ClientID%>', ""); ) this line does the postback not textbox itself. unless you set the property of Autopostback=true ; – nAviD Feb 01 '16 at 20:19
  • I removed that, and still, postBack, i even placed a textbox in an empty page, same result. – Antoine Pelletier Feb 02 '16 at 18:27
  • I saw it's because of the `runat=server` but if i don't specify it, my control is not visible from code behind, so i'm done for – Antoine Pelletier Feb 02 '16 at 18:42
0

Well, textBox automaticaly postBack when enter is pressed, ok then.

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...

Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62