__doPostBack generates JavaScript errors in UserControl encapsulating form

Discussion in 'ASP .Net Building Controls' started by Stephen Miller, May 18, 2004.

  1. 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

    The user control is placed on an aspx page like:

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

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

    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

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

    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("netscape") > -1)
    theform = document.forms["myUC:myForm"];
    else {
    theform = document.myUC:myForm;
    theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
    theform.__EVENTARGUMENT.value = eventArgument;

    The JavaScript error "Expected ';'" occurs at the line 'theform =

    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?


    Stephen Miller, May 18, 2004
    1. Advertisements

  2. Stephen Miller

    Teemu Keiski Guest


    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist

    Teemu Keiski, May 18, 2004
    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.