Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Csharp.net
Hi i have tried the following code on my button click event but it shows an error as Control 'ctl00_ContentPlaceHolder1_grdsalarydetail' of type 'GridView' must be placed inside a form tag with runat=server. i have tried the same logic of code i asp.net with vb which works after adding an overrides method as
 

Response.Clear();
        Response.AddHeader("Content-disposition", "Attachmen;filename=reports.xlsx");
        StringWriter StringWriter = new System.IO.StringWriter();
        HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
        grdsalarydetail.RenderControl(HtmlTextWriter);
        Response.Write(StringWriter.ToString());
        Response.End();
 
but can any one explain in csharp how can i use the same code as i am not getting the correct logic of override method in csharp
 

 
<b><pre lang="vb">Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
        'MyBase.VerifyRenderingInServerForm(control)
    End Sub</pre>
</b>
Posted 3-Dec-12 5:30am
Edited 3-Dec-12 5:34am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

private void ExportGridToExcel(GridView grdGridView, string fileName)
{
Response.Clear();
Response.AddHeader("content-disposition",
string.Format("attachment;filename={0}.xlsx", fileName));
Response.Charset = "";
Response.ContentType = "application/vnd.xlsx";
 
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

grdGridView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
//You have to override this method
public override void VerifyRenderingInServerForm(Control control)
{
 
}
<pre lang="c#">
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Actually, there are a lot of solutions to export data from gridview to Excel. I am afraid people may not successfully reproduce your problem, so why do not try other solutions? they are easy to perform and at the same time, you can learn much from them. Here I can suggest you some, I believe they are helpful for you, you want simple code, so you can decide yourself which is the best
9 Solutions to Export Data to Excel for ASP.NET[^]
Export DataTable to Excel through DataGridView[^]
Excel to Datatable and Datatable to Excel[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 562
1 Sergey Alexandrovich Kryukov 484
2 Maciej Los 325
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100