J
John Hughes
I'm trying to add a user control to a form via the pages render method and I
get the following error :
"Control 'Button1' of type 'Button' must be placed inside a form tag with
runat=server"
The user control has this button on its page. I am outputing the <form
runat="server"> inside the render method and have also tried adding it to
the page and to the control without any luck. Any help appreciated.
Output of the page without the user control being rendered :
<html>
<body><form id="Form1" method="post" runat="server">
<table border=1>
<tr>
<td>
<span id="Label1">my Text</span>
</td>
<td>
</td>
</tr>
</table>
</form></body>
</html>
Render method :
protected override void Render(HtmlTextWriter writer)
{
PlaceHolder myPlaceHolder = new PlaceHolder();
string str = "ZText.ascx";
Control myControl = LoadControl(str);
myPlaceHolder.Controls.Add(myControl);
string str2 = "ZButton.ascx";
Control myControl2 = LoadControl(str2);
myPlaceHolder.Controls.Add(myControl2);
StreamReader sr = new StreamReader(Request.PhysicalApplicationPath +
"PaneLayout.zp");
String line;
while ((line = sr.ReadLine()) != null)
{
if (line.IndexOf("<body>") == 0)
{
writer.Write(line);
writer.Write(@"<form id=""Form1"" method=""post"" runat=""server"">");
writer.WriteLine();
}
else if (line.IndexOf("</body>") == 0)
{
writer.Write(@"</form>");
writer.Write(line);
writer.WriteLine();
}
else if (line.IndexOf("ZText") > 0)
{
myControl.RenderControl(writer);
}
else if (line.IndexOf("ZButton") > 0)
{
myControl2.RenderControl(writer);
}
else
{
writer.Write(line);
writer.WriteLine();
}
}
sr.Close();
base.Render (writer);
}
get the following error :
"Control 'Button1' of type 'Button' must be placed inside a form tag with
runat=server"
The user control has this button on its page. I am outputing the <form
runat="server"> inside the render method and have also tried adding it to
the page and to the control without any luck. Any help appreciated.
Output of the page without the user control being rendered :
<html>
<body><form id="Form1" method="post" runat="server">
<table border=1>
<tr>
<td>
<span id="Label1">my Text</span>
</td>
<td>
</td>
</tr>
</table>
</form></body>
</html>
Render method :
protected override void Render(HtmlTextWriter writer)
{
PlaceHolder myPlaceHolder = new PlaceHolder();
string str = "ZText.ascx";
Control myControl = LoadControl(str);
myPlaceHolder.Controls.Add(myControl);
string str2 = "ZButton.ascx";
Control myControl2 = LoadControl(str2);
myPlaceHolder.Controls.Add(myControl2);
StreamReader sr = new StreamReader(Request.PhysicalApplicationPath +
"PaneLayout.zp");
String line;
while ((line = sr.ReadLine()) != null)
{
if (line.IndexOf("<body>") == 0)
{
writer.Write(line);
writer.Write(@"<form id=""Form1"" method=""post"" runat=""server"">");
writer.WriteLine();
}
else if (line.IndexOf("</body>") == 0)
{
writer.Write(@"</form>");
writer.Write(line);
writer.WriteLine();
}
else if (line.IndexOf("ZText") > 0)
{
myControl.RenderControl(writer);
}
else if (line.IndexOf("ZButton") > 0)
{
myControl2.RenderControl(writer);
}
else
{
writer.Write(line);
writer.WriteLine();
}
}
sr.Close();
base.Render (writer);
}