Professional Web Applications Themes

__doPostBack generates JavaScript errors in UserControl encapsulating form - ASP.NET Building Controls

I have a form shared by several aspx pages. It's the only form appearing on the pages, so I though I would encapsulate the form in a User Control to simplify maintenance. The ascx page looks like: <% Control CodeBehind="myForm.vb" Inherits="Project.myForm" Language="vb" AutoEventWireup="false" %> <form id="myForm" method="post" runat="server"> <asp:datagrid id="myGrid" runat="server" OnSortCommand="myGrid_Sort"> etc </asp:datagrid> </form> The user control is placed on an aspx page like: <% Register TagPrefix="uc" TagName="form" Src="myForm.ascx" %> <body> <uc:form id="myUC" runat="server"></uc:form> </body> Ok now, that's all pretty straightforward. When the page renders, it has to create unique names for the nested controls so they ...

  1. #1

    Default __doPostBack generates JavaScript errors in UserControl encapsulating form

    I have a form shared by several aspx pages. It's the only form
    appearing on the pages, so I though I would encapsulate the form in a
    User Control to simplify maintenance. The ascx page looks like:

    <% Control CodeBehind="myForm.vb" Inherits="Project.myForm"
    Language="vb" AutoEventWireup="false" %>
    <form id="myForm" method="post" runat="server">
    <asp:datagrid id="myGrid" runat="server" OnSortCommand="myGrid_Sort">
    etc
    </asp:datagrid>
    </form>

    The user control is placed on an aspx page like:

    <% Register TagPrefix="uc" TagName="form" Src="myForm.ascx" %>

    <body>
    <uc:form id="myUC" runat="server"></uc:form>
    </body>

    Ok now, that's all pretty straightforward. When the page renders, it
    has to create unique names for the nested controls so they are
    prefixed with the User Controls's id and the resulting html looks
    like:

    <form name="myUC:myForm" method="post" action="myForm.aspx"
    id="myUC_myForm">
    <table id="myUC_myGrid">
    <tr><td><a href="javascript:__doPostBack('myUC$myGrid$_ctl1$_ ctl0','')">Sort</a></td></tr>
    ... etc
    </table>
    </form>

    Now the problem is, that this form generates JavaScript errors on the
    auto generated '__doPostBack' function, apparently because of the semi
    colon on the form name 'myUC:myForm'. The '__doPostBack' function
    generate by ASP.Net looks like:

    function __doPostBack(eventTarget, eventArgument) {
    var theform;
    if (window.navigator.appName.toLowerCase().indexOf("n etscape") > -1)
    {
    theform = doent.forms["myUC:myForm"];
    }
    else {
    theform = doent.myUC:myForm;
    }
    theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
    theform.__EVENTARGUMENT.value = eventArgument;
    theform.submit();
    }

    The JavaScript error "Expected ';'" occurs at the line 'theform =
    doent.forms["myUC:myForm"];'

    If I move the form tags '<form id="myForm" method="post"
    runat="server">' and '</form>' from the ascx to the aspx page, the
    page renders without the semi colon in the form's name and there are
    no JavaScript errors.

    I can't see anyway to resolve this problem, because the semi colon in
    the form name and the __doPostBack function is auto generated by the
    ASP.Net passer. What should I do?

    Thanks,

    Stephen
    Stephen Miller Guest

  2. #2

    Default Re: __doPostBack generates JavaScript errors in UserControl encapsulating form

    [url]http://support.microsoft.com/default.aspx?scid=kb;en-us;817779&Product=aspnet[/url]

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    [url]http://blogs.aspadvice.com/joteke[/url]

    "Stephen Miller" <jsaustenhotmail.com> wrote in message
    news:cdb404de.0405171840.436df099posting.google.c om...
    > I have a form shared by several aspx pages. It's the only form
    > appearing on the pages, so I though I would encapsulate the form in a
    > User Control to simplify maintenance. The ascx page looks like:
    >
    > <% Control CodeBehind="myForm.vb" Inherits="Project.myForm"
    > Language="vb" AutoEventWireup="false" %>
    > <form id="myForm" method="post" runat="server">
    > <asp:datagrid id="myGrid" runat="server" OnSortCommand="myGrid_Sort">
    > . etc
    > </asp:datagrid>
    > </form>
    >
    > The user control is placed on an aspx page like:
    >
    > <% Register TagPrefix="uc" TagName="form" Src="myForm.ascx" %>
    > .
    > <body>
    > <uc:form id="myUC" runat="server"></uc:form>
    > </body>
    >
    > Ok now, that's all pretty straightforward. When the page renders, it
    > has to create unique names for the nested controls so they are
    > prefixed with the User Controls's id and the resulting html looks
    > like:
    >
    > <form name="myUC:myForm" method="post" action="myForm.aspx"
    > id="myUC_myForm">
    > <table id="myUC_myGrid">
    > <tr><td><a
    href="javascript:__doPostBack('myUC$myGrid$_ctl1$_ ctl0','')">Sort</a></td></
    tr>
    > ... etc
    > </table>
    > </form>
    >
    > Now the problem is, that this form generates JavaScript errors on the
    > auto generated '__doPostBack' function, apparently because of the semi
    > colon on the form name 'myUC:myForm'. The '__doPostBack' function
    > generate by ASP.Net looks like:
    >
    > function __doPostBack(eventTarget, eventArgument) {
    > var theform;
    > if (window.navigator.appName.toLowerCase().indexOf("n etscape") > -1)
    > {
    > theform = doent.forms["myUC:myForm"];
    > }
    > else {
    > theform = doent.myUC:myForm;
    > }
    > theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
    > theform.__EVENTARGUMENT.value = eventArgument;
    > theform.submit();
    > }
    >
    > The JavaScript error "Expected ';'" occurs at the line 'theform =
    > doent.forms["myUC:myForm"];'
    >
    > If I move the form tags '<form id="myForm" method="post"
    > runat="server">' and '</form>' from the ascx to the aspx page, the
    > page renders without the semi colon in the form's name and there are
    > no JavaScript errors.
    >
    > I can't see anyway to resolve this problem, because the semi colon in
    > the form name and the __doPostBack function is auto generated by the
    > ASP.Net passer. What should I do?
    >
    > Thanks,
    >
    > Stephen

    Teemu Keiski Guest

Similar Threads

  1. When using rollback feature...Renames file to .newextension and generates errors
    By Reets in forum Macromedia Contribute General Discussion
    Replies: 2
    Last Post: December 13th, 02:55 PM
  2. accessing label in usercontrol from javascript
    By Fabrizio in forum ASP.NET Building Controls
    Replies: 1
    Last Post: February 27th, 03:36 PM
  3. Overwriting the "href='javascript:__doPostBack........" in a LinkButton
    By Jeffrey in forum ASP.NET Building Controls
    Replies: 2
    Last Post: February 18th, 04:11 PM
  4. asp:form generates invalid code
    By Vaclav Jedlicka in forum ASP.NET General
    Replies: 6
    Last Post: July 23rd, 02:22 PM
  5. ANN: Metastorage generates form handling classes
    By Manuel Lemos in forum PHP Development
    Replies: 0
    Last Post: July 10th, 10:41 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139