CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212

    Enter in Textbox reloads page instead of submitting form

    I'm sure this is simple and there's plenty of close-but-not-quite posts...

    I've got a content page containing a LoginView which has got a Textbox and Button on the AnonymousTemplate. I enter "123" in the Textbox and press enter, the page reloads (I've put a trace in the Page_Load event) and the Textbox still contains "123". If I press my Button instead of pressing enter while in the Textbox, it works as I would expect. I've tried adding
    Code:
    ClientScript.RegisterHiddenField("__EVENTTARGET", "MyButtonName");
    (suggested instead of Page.RegisterHiddenField by my compiler) but that made no difference.
    What's going on? How do I get the expected behaviour instead of a page reload?

    Thanks for your help!
    Some cause happiness wherever they go; others, whenever they go.

  2. #2
    Join Date
    Sep 2004
    Posts
    65

    Re: Enter in Textbox reloads page instead of submitting form

    I ran into this kind of problem a little while ago. Here's what I did.

    I defined a javascript function to handle the keypress event in the textbox. In the function, I test if it is enter key, and if so, I set __EVENTTARGET to some value and then submit the form.

    In my Page_Load in the code behind, I check the value of __EVENTTARGET, and if it is what I set it to, then I call the same function that the submit button called.

    my aspx page:
    Code:
    <script language="javascript">
       function SubmitEnter(e)
       {
          if (e.keyCode ==13)
          {
             document.forms("Form1").__EVENTTARGET.value = "submit";
             document.forms("Form1").submit();
          }
       }
    </script>
    
    ... later on...
    
    <asp:textbox id="txtEntry" runat="server" onkeypress="SubmitEnter(event);"></asp:textbox>
    In my codebehind...
    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       if not (Page.IsPostBack) Then
          Call LoadStuff()  'Load drop downs and stuff...
       else
          If (Page.Request.Params("__EVENTTARGET") = "submit") then
             Call CheckInfo()  'This is the function my submit button also calls
          end if
       end if
    End Sub
    HTH

    Ranthalion

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured