Tuesday, June 5, 2012

RenderControl - Master Page Issue


If you are using RenderControl method of a particular control on the child page of Master-Child Page environment like:
        public static string RenderControl(Control control)
        {
            string renderedString;
            using (TextWriter writer = new StringWriter())
            {
                control.RenderControl(new HtmlTextWriter(writer));
                renderedString = writer.ToString();
            }
            return renderedString;
        }
You may get the following error:
Control 'MainContent_xxx' of type 'xxx' must be placed inside a form tag with runat=server
This is because the JIT compiler fails to find <form runat=server> tag on child page that we are using. We can easily inform the JIT that we are in server form just by overriding the VerifyRenderingInServerForm method on our child page.
public override void VerifyRenderingInServerForm(Control control)
{
    // Confirms that an HtmlForm control is rendered for the
    // specified ASP.NET server control at run time.
    // No code required here.
}
This resolves the issue.

No comments:

Post a Comment