i want to implement a print / download csv from each Gridview in my application. Those get their data by Datasources or directly by
gvSample.DataSource = Data;
gvSample.DataBind();
Now my first approach was setting a Download-Button into the Footer-Template and handle the Download there
<asp:GridView ID="gvSample" runat="server">
<PagerTemplate>
<asp:ImageButton ImageUrl="~/download.gif" OnClick="dl_Click" runat="server" ID="dl"/>
</PagerTemplate>
</asp:GridView>
and
protected void dl_Click(object sender, ImageClickEventArgs e)
{
GridView gv = (GridView)this.Parent.Parent.Parent.Parent;
string csv = ToCSV(gv.DataSource); //gv.DataSource is null, DatasourceID aswell
Response.ContentType = "application/csv";
Response.AddHeader("content-disposition", "attachment; filename=file.csv");
Response.Write(csv);
Response.End();
}
but i cant access the data.
DataSource
inGridView
is not stored in any persistent way acrossPostback
so you have to save it somewhere (Viewstate or Session) or you have to request it again from your data-store (Es your db).A quick explanation of the 3 methods:
VievState: is saved in an hidden field in the page so it is not recommended for large data-set because your page could become many MB. It's advantage is that it is saved in the page so it does not expire
Session: is saved in the serve memory so you 'quite' don't have size problems but session does not last forever (usually 30minutes) and if a user display that page and click download after an hour than the session will be null
Request from data-store you request the data from the db so another round trip is done and data could be different respect what the user are seeing
Just a suggestion:
you can access simpler the grid using directly
gvSample
in this way it will be not broken if you cange your html...: