GridView.DataSource是回发期间空 [英] GridView.DataSource is null during PostBack

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

问题描述

我要实现我的应用程序从每个GridView的打印/下载CSV。那些

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

但我不能访问数据。

but i cant access the data.

推荐答案

数据源 GridView控件未存储在横渡回发任何持久的方式,所以你必须从某个地方保存它(的视图状态或的对话),或者你必须从你的数据存储再次请求它(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).

3种方法的简单说明:

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

只是一个建议:

您可以访问更简单,如果你cange你的HTML直接 gvSample 以这种方式将被打破不使用网格...

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是回发期间空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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