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