Override Render - adding user control

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);
}
 
S

S. Justin Gengo

John,

Put a Placholder Control on the page where you want your control to appear.

Then add the control to the Placeholder.

Placeholder.Controls.Add

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

S. Justin Gengo

John,

No, put the placeholder in the page code don't create it in your
code-behind.

<html>
<body><form id="Form1" method="post" runat="server">
<table border=1>
<tr>
<td>
<span id="Label1">my Text</span>
<asp:placeholder id="MyPlaceholder" runat=server>
</td>
<td>
</td>
</tr>
</table>
</form></body>
</html>

Then just add your controls to that.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top