How to master page using export excel in gridview
I Am trying export to excel gridview in master page but error coming below
Control 'ctl00_ContentPlaceHolder1_gvLeavesReport' of type 'GridView' must be placed inside a form tag with runat=server.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Nirmal KumarCPosted Sep 30, 2014, 9:41 AM
Sanjay KumarPosted Sep 29, 2014, 5:29 AM
try this
public override void VerifyRenderingInServerForm(Control control)
{
// this is required for avoid error (control must be placed inside form tag)
}
private void ExportGrid(string fileName, string contentType)
{
Response.Clear();
Response.Buffer =
true;
Response.AddHeader(
"content-disposition", "attachment;filename=" + fileName);
Response.Charset =
"";
Response.ContentType = contentType;
StringWriter sw = new StringWriter();
HtmlTextWriter HW = new HtmlTextWriter(sw);
// Read Style file (css) here and add to response
FileInfo fi = new FileInfo(Server.MapPath("~/css/StyleSheet.css"));
StringBuilder sb = new StringBuilder();
StreamReader sr = fi.OpenText();
while (sr.Peek() >= 0)
{
sb.Append(sr.ReadLine());
}
sr.Close();
grdPaymentDeatils.RenderControl(HW);
Response.Write(
"" + sw.ToString() + "");
Response.Flush();
Response.Close();
Response.End();
}
protected void btnExportExcel_Click(object sender, ImageClickEventArgs e)
{
// Export Gridview to Excel
ExportGrid(
DateTime.Now + "PaymentPaidDetails.xls", "application/vnd.ms-excel");
}