About Enter key in vb.net code behind

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • s2008
    New Member
    • Dec 2008
    • 3

    About Enter key in vb.net code behind

    Hi,
    I'm using vb.net 2005. In my code behind i wrote like this code..
    [code=vbnet]
    Partial Class SearchTxt
    Inherits System.Web.UI.P age
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load

    End Sub
    Protected Sub TextBox1_TextCh anged(ByVal sender As Object, ByVal e As System.EventArg s) Handles TextBox1.TextCh anged
    If Not TextBox1.Text = "" Then
    Response.Redire ct("Page2.aspx? txt=" & TextBox1.Text)
    End If
    End Sub
    End Class
    [/code]
    Now what i want is i want the page to redirect only if the enter key is pressed inside the textbox...Pleas e tell me how to write the code inside TextBox1_TextCh anged..
    Last edited by Frinavale; Dec 18 '08, 06:57 PM. Reason: added [code] tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You can only check which key is pressed in client side code.
    So you're going to have to write JavaScript that checks to see if the enter key is pressed when the user is in the text box. You're going to have to write JavaScript functions that handle the OnBlur, and OnChange events to do this:

    OnChange event: you should check to see which key the user's entered...if the user's hit the enter key, store a "True" value in a hidden field that you can then retrieve in the server code so that you can determine what to do.

    OnBlur event: store a "False" value in the hidden field in order to indicate that you are no longer in the text box.

    -Frinny

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Pressing enter on an input field just fires a postback. None of your specific button events should fire (unless there is a default button assigned to the form)

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by Plater
        Pressing enter on an input field just fires a postback. None of your specific button events should fire (unless there is a default button assigned to the form)
        If you set the function that handles OnChange event to return False... and in that function catch the case when the enter key is hit I think it should prevent it from firing....regar dless I think the OnChange event happens before the enter key gets to submit the forum.

        I'm going to try it now.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          The following JavaScript checks if the user pressed Enter:
          [code=javascript]
          function checkEnter(e)
          {
          var unicode;
          try
          { /*IE*/
          unicode = e.keyCode;
          }
          catch(err)
          {
          try
          { /*Netscape, Mozilla, FireFox...*/
          unicode = window.event.ke yCode;

          }
          catch(error)
          { /*Other*/
          unicode = e.which;
          }
          }
          //displays an alert and returns false
          //if enter is pressed
          if(unicode == 13)
          { alert("enter pressed");
          return false;
          }
          return true;
          }[/code]

          In my ASP code I created a TextBox
          [code=asp]
          <asp:TextBox ID="testTextBox " runat="server"> </asp:TextBox>[/code]

          And in my PageLoad event I set the TextBox to execute the function every time a key's pressed:

          [code=vbnet]
          Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load
          If IsPostBack = False Then
          testTextBox.Att ributes.Add("on keypress", "return checkEnter(even t);")
          End If
          End Sub
          [/code]

          Note that I set the "onkeypress " event to return the function call. The enter action is cancelled because I'm returning false here...

          So this suggests that the JavaScript is executed before the page is sent to the server....
          -Frinny

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            isn't window.event just IE?
            Javascript - Event accessing

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Originally posted by Plater
              isn't window.event just IE?
              Javascript - Event accessing
              Hmmm....
              Apparently not because I tested that in FireFox.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Hmm everything I read about event handling says mozilla based uses the "e" but in IE you could use window.event

                [code=javascript]
                function OnlyNumbers(e)
                {
                var keynum;
                if(window.event ) // IE
                {
                keynum = e.keyCode;
                }
                else if(e.which) // Netscape/Firefox/Opera
                {
                keynum = e.which;
                }
                //...
                }
                [/code]

                Although I am sure they have unified somewhat in their behavior

                Comment

                Working...