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)
{

}


Related Articles

Export Data to Excel in CSV format using .NET

Export Data to excel in CSV format. .NET code to export CSV data. Free .NET code to export data to excel. .NET way of exporting data to excel. Explanation of .NET code to export data to excel in CSV.

More articles: Export to Excel

Comments

Author: JimmyTong18 Aug 2010 Member Level: Bronze   Points : 1



sometime I got error 'no suitable method found to override'
when use
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
can U help me?

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: