473,503 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Some controls cannot RenderControl

Hi group,

I'm trying to get the outputted html from some specific controls on a page,
and I'm finding that some controls will render fine, while others will throw
the error that "[control] must be placed inside a form tag with
runat=server." For example, a label control will render fine, but a button
will not. It seems that any control that has a server-side click event will
not accept being rendered in this way. Does anyone know what I can do to
get such controls to render? Here's my code:

aspx snippet:

<form id="f" runat="server">
<div>
<asp:Label ID="control" runat="server" Text="Chage to an asp:Button"
/>
</div>
</form>
..cs snippets:

using System;
using System.Web.UI;
using System.IO;

// ...

using (StringWriter sw = new StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter(sw);
control.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

Thanks for any help.

Joshua C

Jul 9 '07 #1
2 5307
Hi there,

I'm afraid once control is put on the page, its Page & Parent property is
assigned, and here's a statement that prevents control from being rendered:

bool useSubmitBehavior = this.UseSubmitBehavior;
if (this.Page != null)
{
this.Page.VerifyRenderingInServerForm(this);
}

There is no problem in assigning Page to null, unfortunatelly, if Page is
null, Parent.Page is returned:

public virtual Page Page
{
get
{
if ((this._page == null) && (this.Parent != null))
{
this._page = this.Parent.Page;
}
return this._page;
}
set
{
if (this.OwnerControl != null)
{
throw new InvalidOperationException();
}
this._page = value;
}
}

Unfortunatelly, Parent property is readonly so cannot interfere with its
value (actually you could using Reflection but it would get nasty). Anyway,
it's gonna work if you create controls dynamically, i.e.
Button btn = new Button();
btn.Name = "btn";
btn.ID= "btn";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter(sw);
btn.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

but not if the button was placed on the page.

Regards
--
Milosz
"Josh Carver" wrote:
Hi group,

I'm trying to get the outputted html from some specific controls on a page,
and I'm finding that some controls will render fine, while others will throw
the error that "[control] must be placed inside a form tag with
runat=server." For example, a label control will render fine, but a button
will not. It seems that any control that has a server-side click event will
not accept being rendered in this way. Does anyone know what I can do to
get such controls to render? Here's my code:

aspx snippet:

<form id="f" runat="server">
<div>
<asp:Label ID="control" runat="server" Text="Chage to an asp:Button"
/>
</div>
</form>
..cs snippets:

using System;
using System.Web.UI;
using System.IO;

// ...

using (StringWriter sw = new StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter(sw);
control.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

Thanks for any help.

Joshua C

Jul 9 '07 #2
Thanks a lot for your help, Milosz. I was actually able to get it to work
without loading the control dynamically. I just added:

EnableEventValidation="false"

to the page directive. And I added this to the code-behind after reading
what you wrote:

public override void VerifyRenderingInServerForm(Control control)
{
return;
}

Thanks again!

Josh
"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:C0**********************************@microsof t.com...
Hi there,

I'm afraid once control is put on the page, its Page & Parent property is
assigned, and here's a statement that prevents control from being
rendered:

bool useSubmitBehavior = this.UseSubmitBehavior;
if (this.Page != null)
{
this.Page.VerifyRenderingInServerForm(this);
}

There is no problem in assigning Page to null, unfortunatelly, if Page is
null, Parent.Page is returned:

public virtual Page Page
{
get
{
if ((this._page == null) && (this.Parent != null))
{
this._page = this.Parent.Page;
}
return this._page;
}
set
{
if (this.OwnerControl != null)
{
throw new InvalidOperationException();
}
this._page = value;
}
}

Unfortunatelly, Parent property is readonly so cannot interfere with its
value (actually you could using Reflection but it would get nasty).
Anyway,
it's gonna work if you create controls dynamically, i.e.
Button btn = new Button();
btn.Name = "btn";
btn.ID= "btn";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter(sw);
btn.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

but not if the button was placed on the page.

Regards
--
Milosz
"Josh Carver" wrote:
>Hi group,

I'm trying to get the outputted html from some specific controls on a
page,
and I'm finding that some controls will render fine, while others will
throw
the error that "[control] must be placed inside a form tag with
runat=server." For example, a label control will render fine, but a
button
will not. It seems that any control that has a server-side click event
will
not accept being rendered in this way. Does anyone know what I can do to
get such controls to render? Here's my code:

aspx snippet:

<form id="f" runat="server">
<div>
<asp:Label ID="control" runat="server" Text="Chage to an
asp:Button"
/>
</div>
</form>
..cs snippets:

using System;
using System.Web.UI;
using System.IO;

// ...

using (StringWriter sw = new StringWriter())
{
HtmlTextWriter htw = new HtmlTextWriter(sw);
control.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

Thanks for any help.

Joshua C

Jul 10 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2930
by: doron | last post by:
Hi, I'm tryig to add a LinkButton with events to a custom control (custom control : Datagrid) My CreateChildControls is generating an error (Specified cast is not valid.)
3
2627
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event...
1
3136
by: sleigh | last post by:
Hello, I'm building a web application that will build a dynamic form based upon questions in a database. This form will have several different sections that consist of a panel containing one to...
2
2205
by: Juan Romero | last post by:
Hey guys, I am working on a web custom control that basically draws a table (ASP Table) with a few child controls in the cells. I have a command button inside one of these cells. The problem I...
2
4762
by: Tom Bray | last post by:
Ok I am baffled I can not figure out this problem. I am getting the following error: Portal Error - A DropDownList cannot have multiple items selected. Error information Name Value Message...
0
1650
by: Earl Teigrob | last post by:
I have created a Custom Control that accepts an array of Objects and uses the contents of those objects to add controls to its child control struture and have them rendered to the display. I also...
5
5123
by: Nathan Sokalski | last post by:
When running a page I am working on, I recieve the following error: Cannot use a leading .. to exit above the top directory. I suspect this has something to do with the problem I posted in a...
1
1538
by: estebistec | last post by:
Hello, I've developed a CompositeControl in C# for ASP.NET 2.0. In this control I am simulating Edit and View modes by hiding or showing appropriate controls (setting Control.Visible). Basically...
2
3574
by: Nigil | last post by:
Hi Guys, Does anyone know how to RenderControl with sub controls? cos I am having issues with that. Following is my code. Inside my UserControls, I have a Repeater. <form method="post"...
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.