Error using GridView's RenderControl property during Export to Excel

When you try to Export the results from a GridView to excel using the GridView's RenderControl property, one might face an error stating that the server control must be placed inside a form tag with runat="server".

We can overcome this problem by ovverriding the Page.VerifyRenderingInServerForm() Method. This confirms that for a specified ASP.NET server control an HtmlForm control is rendered at run time.

Following is a sample piece of code:

protected void btnExport_Click(object sender, EventArgs e)
{
System.IO.StringWriter objStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter objHtmlTextWriter = new System.Web.UI.HtmlTextWriter(objStringWriter);

GridView1.RenderControl(objHtmlTextWriter);
Response.Write(objStringWriter.ToString());
Response.End();
}


public override void VerifyRenderingInServerForm(Control control)
{

}


Comments

Author: tadelasatishkumar26 Aug 2010 Member Level: Gold   Points : 0

Check whether u have written



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: