must be placed inside a form tag with runat=server” error

Sometimes you can get “…must be placed inside a form tag with runat=server” error, when you deal with “asp:panel” or “asp:linkbutton” or whatever else scenarious when html form control needs to be rendered for the specified control at run time. The problem can be solved (thanks, Alex) ovverridingPage.VerifyRenderingInServerForm Method. Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.

So, the code would look like the following (rendering asp:panel containing controls itself):

private void Page_Load(object sender, EventArgs e)
{
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    contentPanel.RenderControl(htmlWriter);
    string s = stringWriter.ToString();

    Response.Write(s);
    Response.End();
}

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s