By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,665 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,665 IT Pros & Developers. It's quick & easy.

Override Render - adding user control

P: n/a
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);
}
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"John Hughes" <jh******@msn.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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);
}

Nov 19 '05 #2

P: n/a
I'm already using a placeholder control to place my user controls.
"S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
message news:O%****************@tk2msftngp13.phx.gbl...
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
"John Hughes" <jh******@msn.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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);
}


Nov 19 '05 #3

P: n/a
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
"John Hughes" <jh******@msn.com> wrote in message
news:OG**************@TK2MSFTNGP09.phx.gbl...
I'm already using a placeholder control to place my user controls.
"S. Justin Gengo" <sjgengo@[no_spam_please]aboutfortunate.com> wrote in
message news:O%****************@tk2msftngp13.phx.gbl...
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
"John Hughes" <jh******@msn.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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);
}



Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.