HELP: [RETURN] key is not submitting form (ASP.NET 2.0)

Discussion in 'ASP .Net' started by Axel Dahmen, Feb 20, 2007.

  1. Axel Dahmen

    Axel Dahmen Guest

    Hi,

    trying to submit an ASPX form using the [RETURN] key (using IE6) the page is not submitted in my web project.

    Trying to debug the pages' JavaScript code I noticed that there's some ASP.NET client script code being executed having a flaw:

    function anonymous() {
    if (!ValidatedTextBoxOnKeyPress(event)) { event.cancelBubble = true; if (event.stopPropagation) event.stopPropagation(); return false; }
    }

    In this code, not all paths return a value! So my guess is that a random value is used by IE to consider whether to submit the form.

    If this is true then I guess there's a bug in ASP.NET blocking me from implementing my customer's requirements.

    Any ideas?

    TIA,
    Axel Dahmen
     
    Axel Dahmen, Feb 20, 2007
    #1
    1. Advertisements

  2. Axel Dahmen

    bruce barker Guest

    there is nothing wrong with the code. it returns void if no validtion is
    required or validation is ok, or false if validation fails (and also as
    a hack for an ie 6 bug in event canceling), which is what the browser wants.

    if you look asp.net has default button support (which button to fire on
    enter). see panel control.

    -- bruce (sqlwork.com)
     
    bruce barker, Feb 20, 2007
    #2
    1. Advertisements

  3. Axel Dahmen

    Axel Dahmen Guest

    I see... Didn't know void was a legal return value.

    It seems the script debugger doesn't step through dynamically generated code (new Function(){...}). In that case I now know where to search now...

    Thanks, Bruce!

    Regards,
    Axel

    -----------
     
    Axel Dahmen, Feb 20, 2007
    #3
  4. Hi Alex,

    Both the HtmlForm and the Panel have a property named DefaultButton could
    let you assign a default button when ENTER key is pressed:

    <form id="form1" defaultbutton="Button1" runat="server">
    <div>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
    Text="Button" />
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <asp:panel ID="Panel1" DefaultButton="Button2" runat="server"
    Height="50px" Width="125px">
    <asp:Button ID="Button2" runat="server" OnClick="Button1_Click"
    Text="Button" />
    <asp:TextBox ID="TextBox2"
    runat="server"></asp:TextBox></asp:panel>

    </div>
    <asp:panel ID="Panel2" DefaultButton="Button3" runat="server"
    Height="50px" Width="125px">
    <asp:Button ID="Button3" runat="server" OnClick="Button1_Click"
    Text="Button" />
    <asp:TextBox ID="TextBox1"
    runat="server"></asp:TextBox></asp:panel>
    </form>



    protected void Button1_Click(object sender, EventArgs e)
    {
    Response.Write((sender as Button).ID);
    }



    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Feb 21, 2007
    #4
  5. Axel Dahmen

    Axel Dahmen Guest

    Hi Walter,

    great to hear from you! I guess the DefaultButton property doesn't work for LinkButtons, does it? Our website uses LinkButtons exclusively. We're applying the company's style guide to them.

    I've created a function to scan Request.Form["__EVENTTARGET"] for not being any of a page's link buttons on post back. This code raises a DefaultButtonPressed event appropriately.

    Yet, unfortunately the form doesn't post back at all when I press the [RETURN] key at the moment... I've created a second function to inject code into the document.forms[0].onsubmit event handler. Perhaps the code I'm injecting there doesn't work properly... I needed to write custom code since Page.ClientScript.RegisterOnSubmitStatement injects code before validation but we want to get code executed *after* validation has been performed successfully. Perhaps you've got an idea?

    Best regards,
    www.dashop.de
    Axel Dahmen
     
    Axel Dahmen, Feb 21, 2007
    #5
  6. Hi Axel,

    Thanks for the update.

    For the first part about LinkButton, based on my test, if the form has a
    defaultButton set, focus on a LinkButton and press ENTER will still make
    the defaultButton to submit the form. Also, if you set the defaultButton
    attribute of the form to a LinkButton, it also works. I'm not sure if I
    fully understood your question here, would you please elaborate more?

    For the second part of the question, do you mean that you want to execute
    some custom javascript code after the page's client-side validation passes?
    I'm afraid it will not be easy to do so. Let's take a look at the
    javascript generated by ASP.NET (view the webform in IE and save it as a
    complete static html page to get the javascript files):

    function WebForm_PostBackOptions(eventTarget, eventArgument, validation,
    validationGroup, actionUrl, trackFocus, clientSubmit) {
    this.eventTarget = eventTarget;
    this.eventArgument = eventArgument;
    this.validation = validation;
    this.validationGroup = validationGroup;
    this.actionUrl = actionUrl;
    this.trackFocus = trackFocus;
    this.clientSubmit = clientSubmit;
    }
    function WebForm_DoPostBackWithOptions(options) {
    var validationResult = true;
    if (options.validation) {
    if (typeof(Page_ClientValidate) == 'function') {
    validationResult = Page_ClientValidate(options.validationGroup);
    }
    }
    if (validationResult) {
    if ((typeof(options.actionUrl) != "undefined") &&
    (options.actionUrl != null) && (options.actionUrl.length > 0)) {
    theForm.action = options.actionUrl;
    }
    if (options.trackFocus) {
    var lastFocus = theForm.elements["__LASTFOCUS"];
    if ((typeof(lastFocus) != "undefined") && (lastFocus != null)) {
    if (typeof(document.activeElement) == "undefined") {
    lastFocus.value = options.eventTarget;
    }
    else {
    var active = document.activeElement;
    if ((typeof(active) != "undefined") && (active !=
    null)) {
    if ((typeof(active.id) != "undefined") &&
    (active.id != null) && (active.id.length > 0)) {
    lastFocus.value = active.id;
    }
    else if (typeof(active.name) != "undefined") {
    lastFocus.value = active.name;
    }
    }
    }
    }
    }
    }
    if (options.clientSubmit) {
    __doPostBack(options.eventTarget, options.eventArgument);
    }
    }



    <A
    id=LinkButton1
    href='javascript:WebForm_DoPostBackWithOptions(new
    WebForm_PostBackOptions("LinkButton1", "", true, "", "", false,
    true))'>LinkButton</A>


    As you can see, the validation and the postback steps are currently
    executed in a single javascript function WebForm_DoPostBackWithOptions. You
    might want to replace the __doPostBack function to do your own stuff but I
    haven't tested this. The default __doPostBack implementation is generated
    in the webform as:

    <SCRIPT type=text/javascript>
    <!--
    var theForm = document.forms['form1'];
    if (!theForm) {
    theForm = document.form1;
    }
    function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
    theForm.__EVENTTARGET.value = eventTarget;
    theForm.__EVENTARGUMENT.value = eventArgument;
    theForm.submit();
    }
    }
    // -->
    </SCRIPT>


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Feb 22, 2007
    #6
  7. Hi Axel,

    Please ignore second part of my last reply since I've got some updated
    information.

    To execute some javascript before submitting the form and after the
    client-side validation succeeds, you simply call some javascript in form's
    onsubmit handler:

    #Followers of the IHttpHandler : No more hijacking of __doPostBack in
    Whidbey
    http://weblogs.asp.net/vga/archive/2004/03/01/NoMoreHijackingOfDoPostBackInW
    hidbey.aspx

    #onsubmit Event (FORM)
    http://msdn2.microsoft.com/en-us/library/ms536972.aspx


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Feb 23, 2007
    #7
  8. Axel Dahmen

    Axel Dahmen Guest

    Hi Walter,

    thank you for your reply. I'll try to use DefaultButton in our forms 'till
    end of the week. Can't tell if it works for us right now although I'm rather
    optimistic that it'll work.

    The other solution, though, doesn't work for me as we're already using
    Whidbey. The problem with the Page.RegisterOnSubmitStatement() member
    function is that it inserts code *before* validators are executed. Yet, I
    need to execute some code *after* validators have (successfully) executed
    because I need to show a "please wait..." <div> element.


    This is what Page.RegisterOnSubmitStatement() does (basically):

    form.onsubmit()
    {
    -- RegisterOnSubmitStatement inserts code here --
    if (!PageIsValid()) return false;
    return true;
    }


    yet, what I need is:

    form.onsubmit()
    {
    if (!PageIsValid()) return false;
    -- insert code here --
    return true;
    }


    Currently I'm injecting this code manually using regular expressions and
    creating a new Form object having my code inserted at the right place.
    Unfortunately I can't debug into the so created function code.

    Do you know any other way to have code inserted where I need it? Or would
    this be something to suggest to the VS team?

    TIA,
    Axel Dahmen

    PS.: Please remember I can't reply to your posts from my customer's office.
    They have blocked the protocol back in Frankfurt.
     
    Axel Dahmen, Feb 27, 2007
    #8
  9. Hi Axel,

    To achieve your objective here (only execute some code after the page is
    validated at client-side), don't use RegisterOnSubmitStatement here (by the
    way, Page.RegisterOnSubmitStatement is obsolete now in .NET 2.0, try to use
    ClientScriptManager.RegisterOnSubmitStatement instead).


    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
    // ClientScript.RegisterOnSubmitStatement(this.GetType(), "submit",
    "return confirm('Continue?')");
    form1.Attributes.Add("onsubmit", "return confirm('Continue?')");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    Response.Write("Button1_Click");
    }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
    runat="server" ControlToValidate="TextBox1"

    ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
    <asp:Button ID="Button1" runat="server" Text="Button"
    OnClick="Button1_Click" /></div>
    </form>
    </body>
    </html>


    Using above test webform, the confirmation prompt will only display when
    the client-side validation passed.

    Let me know if this is what you want.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Mar 1, 2007
    #9
  10. Axel Dahmen

    Guest Guest

    Hi Walter,

    thanks a lot for the response you've mailed to me. Your reply has put me on
    the right path and it's helping me a lot indeed in finding the problem.

    In your solution you wrote that there's an (apparently) invisible validator
    keeping the page from submitting. I'll try to debug the .html pages to see
    which validator that is. Perhaps some CSS is keeping the validator from
    showing up.

    Currently I don't see how I can possibly debug through the JavaScript that's
    generated by the .axd files. Is there a way to debug through this code so I
    can find the problem without first exporting the page into HTML using FireFox
    (since IE doesn't do this)?

    Walter, I really appreciate the effort you've already put into solving our
    problem. Thanks a lot!

    TIA,
    Axel
     
    Guest, Mar 28, 2007
    #10
  11. Hi Axel,

    Yes you can debug the javascript files generated by WebResource.axd. You
    will need to install Microsoft Script Debugger
    (http://www.microsoft.com/downloads/details.aspx?familyid=2f465be0-94fd-4569
    -b3c4-dffdf19ccd99&displaylang=en) and select IE's menu "View/Script
    Debugger/Break at next statement", then after you clicking on one of your
    submit buttons, if there's any validators the javascript should break and
    you can use F11 to step into those functions.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Mar 28, 2007
    #11
  12. Hi Axel,

    How's everything going at your side? Please feel free to let me know if
    there's anything I can help. Thanks.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Apr 2, 2007
    #12
  13. Axel Dahmen

    Guest Guest

    Hi Walter,

    can we get this topic back on? I've had some spare time to debug through the
    JavaScript generated. From what I saw is that all of the validators return
    IsValid=true. Still the page does not submit when I hit the Return key in any
    of the input fields.

    When debugging, ValidatedTextBoxOnKeyPress(event) returns true, so

    function anonymous() {
    if (!ValidatedTextBoxOnKeyPress(event)) { event.cancelBubble = true; if
    (event.stopPropagation) event.stopPropagation(); return false; }
    }

    just jumps to the final curly bracket. That's the last JavaScript I see when
    stepping through the code.

    Do you have any clue whatsoever why IE6 still does not submit the form?

    Your valuable help is quite appreciated!

    TIA,
    Axel Dahmen
     
    Guest, Apr 20, 2007
    #13
  14. Hi Axel,

    Based on my test, those 4 fields will display an "*", which means the
    fields input are not valid. Would you please give me some validated values
    for those 4 fields so that I can test it on my side?

    The 4 fields on page Quickcheck.aspx.htm:

    * Gesamtkosten (inkl. Nebenkosten)
    * Eigenkapitaleinsatz
    * Eigenleistung
    * Jahresnettohaushaltseinkommen


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Apr 23, 2007
    #14
  15. Hi Axel,

    Have you seen my last reply?

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Apr 30, 2007
    #15
  16. Axel Dahmen

    Guest Guest

    Hi Walter,

    I'm sorry, I didn't see your reply until now. In fact, empty values are
    valid values for this form. There's no RequiredFieldValidator attached to any
    of those fields. So leaving them empty should do the job. On the other hand,
    these are all currency values so any integer value will do (in Germany we use
    the comma as decimal separator, but ingeters will do anyway).

    Does this information help you?

    Best regards,
    Axel Dahmen
     
    Guest, May 29, 2007
    #16
  17. Axel Dahmen

    Guest Guest

    ...oops.. Forgot to check the "notify me" checkbox... Done now..
     
    Guest, May 29, 2007
    #17
  18. Hi Axel,

    I'm afraid there's something missing here: with the saved html page, I
    found it failed to postback if the fields are empty and the "*" marks are
    shown to indicate they not valid. That's why I assumed the page is actually
    working fine: it doesn't postback because the client-side validation
    doesn't pass.

    Since this test result is different from yours, I think there might be some
    other causes.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], May 30, 2007
    #18
  19. Hi Axel,

    I'm afraid there's something missing here: with the saved html page, I
    found it failed to postback if the fields are empty and the "*" marks are
    shown to indicate they not valid. That's why I assumed the page is actually
    working fine: it doesn't postback because the client-side validation
    doesn't pass.

    Since this test result is different from yours, I think there might be some
    other causes.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], May 30, 2007
    #19
    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.