Note: VerifyRenderingInServerForm and PrepareGridViewForExport are necessary functions..add as it is without change..else error ll produce
protected void lnkExport_Click(object sender, EventArgs e)
{
if (gv.Rows.Count > 0)
{
Response.Buffer = true;
string attachment = "attachment; filename=Excel.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
else
{
lblError.Text="No Records Found";
}
}
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}
private void PrepareGridViewForExport(System.Web.UI.Control gv)
{
LinkButton lb = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
PrepareGridViewForExport(gv.Controls[i]);
}
}
Incubator, if this helps please login to Mark As Answer. | Alert Moderator