Response.Redirect not working when pressing the Enter key

Discussion in 'ASP .Net' started by nkoier, Feb 14, 2007.

  1. nkoier

    nkoier Guest

    Hi, I've been going crazy trying to figure out what's wrong with our
    Asp.Net 2.0 intranet site.

    At the very top of our main page I provide a TextBox and a Button for
    submitting Google searches. Recently somebody pointed out to me that
    you can't just press ENTER anymore after typing in the TextBox but
    that you HAVE to click on the submit Button WITH THE MOUSE for it to
    work. After some initial troubleshooting I discovered that it's only
    an issue in IE 6 & 7 (works fine in FireFox!).

    To simplify I created a brand new empty web form. I dropped an
    <asp:TextBox/> & <asp:Button/> control on the the page (even made sure
    they had the same ValidationGroup="google") and then wired up the
    Button's click event.

    protected void Button1_Click(object sender, EventArgs e)
    {
    Response.Redirect("http://www.google.com/search?q=" +
    TextBox1.Text);
    }

    If you click the submit Button using the mouse the Response.Redirect
    works fine. If you press ENTER on the keyboard while the focus is
    still in the TextBox it does not work (the page just flashes/posts
    back).

    Let me clarify that the code in the Button1_Click event handler DOES
    execute in both situations. Meaning if I put a breakpoint on the
    Response.Redirect line and then run the debugger; when I type some
    text into the TextBox and press ENTER on the keyboard the breakpoint
    DOES get hit. When I step-out however, the page just refreshes and is
    still sitting there. It's like the code is actually running ok and the
    redirect is being sent to the client, but IE is then stopping it from
    happening at the client-side somehow!? Alternatively, if I type into
    the TextBox then do a SHIFT+TAB to put the focus on the submit button
    itself, THEN pressing ENTER works.

    How can I make it so that someone can type some text into a TextBox
    and then just press ENTER on the keyboard? Anybody have any ideas?
    Please?

    Thanks in advance,
    -Nick
     
    nkoier, Feb 14, 2007
    #1
    1. Advertisements

  2. Try using the DefaultButton attribute of the form tag. Here is an example
    that I used for a login page I wrote:

    <form id="form1" runat="server" defaultbutton="btnSubmit"
    defaultfocus="txtUsername">

    The DefaultButton is the ID of the Button you want "clicked" when the user
    presses enter, and the DefaultFocus is the TextBox the the cursor is
    initially in when the page first loads. Good Luck!
     
    Nathan Sokalski, Feb 14, 2007
    #2
    1. Advertisements

  3. nkoier

    nkoier Guest

    Hey thanks for the help! Your suggestion led me down the right path to
    fixing the problem, or at least patching it.

    I tried to use the DefaultButton property on the Form tag but that
    didn't work, oddly enough. I then tried wrapping the controls in a
    Panel and set the Panel's DefaultButton and that seemed to get it
    working in IE.

    I am still left wondering what was going wrong... Before I even tried
    to set the DefaultButton property it WAS submitting the Form and the
    Button1_Click event WAS firing and the Response.Redirect() WAS being
    executed on the server. When the WebResponse was recieved by IE it
    seems like it just decided to ignore the redirect!? Why did it work
    fine with FireFox and not in IE? I would like to understand exactly
    what was going on behind the scenes. I hate not understanding why.
     
    nkoier, Feb 14, 2007
    #3
  4. I understand how you feel, I usually like to understand the reasons for what
    happens as well. I cannot say I know the answer to your question, but it
    sounds to me like it might be partially browser related, since you are
    mentioning both IE and FireFox. However, I do not have much experience when
    it comes to the differences in the most popular browsers. It might also help
    both me and other people trying to help you with this problem if you posted
    your exact code. Although it is not very efficient, an alternative attemp to
    solving your problem (although I have never tried it) would be to use the
    JavaScript onKeyPress event to detect when the Enter key is pressed, and
    execute the JavaScript form1.submit() method if event.keyCode (if using IE)
    or event.which (for other browsers) is the keycode for the Enter key. Good
    Luck!
     
    Nathan Sokalski, Feb 15, 2007
    #4
  5. Can you please submit the code of how did you wrap the controls in a
    panel. I have similar problem that with IE the response.redirect() is
    not working but with Firefox it does.
    - Sourik
     
    souriksamaddar, Mar 7, 2007
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.