__doPostBack generates JavaScript errors in UserControl encapsulating form

S

Stephen Miller

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

The JavaScript error "Expected ';'" occurs at the line 'theform =
document.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
 
T

Teemu Keiski

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

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

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

The JavaScript error "Expected ';'" occurs at the line 'theform =
document.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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top