GridView.DataSource 在 PostBack 期间为 null [英] GridView.DataSource is null during PostBack

查看:13
本文介绍了GridView.DataSource 在 PostBack 期间为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的应用程序中的每个 Gridview 实现打印/下载 csv.那些通过 Datasources 或直接通过

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>

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();           
}

但我无法访问数据.

推荐答案

DataSourceGridView 中没有以任何持久的方式存储在 Postback所以你必须把它保存在某个地方(ViewstateSession) 或者您必须从您的数据中再次请求它-存储(Es 你的数据库).

DataSource in GridView is not stored in any persistent way across Postback 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:保存在页面的隐藏字段中,因此不推荐用于大型数据集,因为您的页面可能会变得很多 MB.优点是保存在页面中,不会过期

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

ViewState["Data"] = GetData();
gvSample.DataSource = ViewState["Data"];
gvSample.DataBind();

...

protected void dl_Click(object sender, ImageClickEventArgs e)
{
    string csv = ToCSV(ViewState["Data"]);
    ...   
}

会话:保存在服务内存中,因此您完全"没有大小问题,但会话不会永远持续(通常为 30 分钟),如果用户显示该页面并在之后单击下载一个小时的会话将是空的

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

Session["Data"] = GetData();
gvSample.DataSource = Session["Data"];
gvSample.DataBind();

...

protected void dl_Click(object sender, ImageClickEventArgs e)
{
    string csv = ToCSV(Session["Data"]);
    ...   
}

来自数据存储的请求您从数据库请求数据,以便完成另一次往返,并且数据可能会有所不同,尊重用户所看到的

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

gvSample.DataSource = GetData();
gvSample.DataBind();

...

protected void dl_Click(object sender, ImageClickEventArgs e)
{
    string csv = ToCSV(GetData());
    ...   
}

只是一个建议:

您可以直接使用 gvSample 访问更简单的网格,这样如果您更改 html,它就不会被破坏...:

you can access simpler the grid using directly gvSample in this way it will be not broken if you cange your html...:

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
    string csv = ToCSV(gvSample.DataSource);
    ...   
}

这篇关于GridView.DataSource 在 PostBack 期间为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆