Connecting Tech Pros Worldwide Forums | Help | Site Map

Some controls cannot RenderControl

Josh Carver
Guest
 
Posts: n/a
#1: Jul 9 '07
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


=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
 
Posts: n/a
#2: Jul 9 '07

re: Some controls cannot RenderControl


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:
Quote:
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
>
>
Josh Carver
Guest
 
Posts: n/a
#3: Jul 10 '07

re: Some controls cannot RenderControl


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]" <mily242@DONTLIKESPAMwp.plwrote in message
news:C0B53077-0454-4B91-A0E3-21AA3056C951@microsoft.com...
Quote:
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:
>
Quote:
>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
>>
>>
Closed Thread