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.

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.

    ReplyDelete