Step 1:- Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="
Default.aspx.cs" Inherits="Demo.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView
must be placed inside a form tag</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Export
To Excel" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Step 2:- Default.aspx.cs
using System;
using System.Data;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
namespace Demo
{
public partial class Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected DataTable GeTable()
{
DataTable dt
= new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
int totalRow
= 10;
for (int i
= 0; i < totalRow; i++)
{
dr = dt.NewRow();
dr["Column1"] = "Row" + i;
dr["Column2"] = "Row" + i;
dr["Column3"] = "Row" + i;
dr["Column4"] = 1000 / (i + 1);
dt.Rows.Add(dr);
}
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataSource = GeTable();
GridView1.DataBind();
StringWriter sw
= new StringWriter();
HtmlTextWriter htw
= new HtmlTextWriter(sw);
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment;
filename={0}", "ExportedData.xls"));
Response.ContentType = "application/ms-excel";
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
}
}
Step 3:- When you run the project and click on the Export to Excel
Button you will get below Error...
Control GridView1' of type 'GridView' must be placed inside a form
tag with runat=server.
You could avoid this exception by overriding VerifyRenderingInServerForm or Using HtmlForm class.
Step 1:- using overriding VerifyRenderingInServerForm on
the page
public override void VerifyRenderingInServerForm(Control control)
{
}
Step 2:- using HtmlForm class
HtmlForm hf
= new HtmlForm();
Page.Controls.Add(hf);
hf.Controls.Add(GridView1);
hf.RenderControl(htw);
Note:-
If you are using the HtmlForm class, your codes look like
this.
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataSource = GeTable();
GridView1.DataBind();
StringWriter sw
= new StringWriter();
HtmlTextWriter htw
= new HtmlTextWriter(sw);
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment;
filename={0}", "ExportedData.xls"));
Response.ContentType = "application/ms-excel";
HtmlForm hf = new HtmlForm();
Page.Controls.Add(hf);
hf.Controls.Add(GridView1);
hf.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
Thank you so much sir. the above post really helped me and save at least 2 hour
ReplyDelete