.net GridView 在回发期间丢失数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4509676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:03:36  来源:igfitidea点击:

GridView loses data during postback

.netgridviewpostback

提问by AGuyCalledGerald

I have an aspx.Page containing a gridview. The gridview is bound in code behind to a datasource only when no postback takes place and has enableviewstate = true (The page too). During a postback, the data bound to the gridview are lost. What could be the reason. Please ask if code is needed.

我有一个包含 gridview 的 aspx.Page。只有当没有回发发生并且具有 enableviewstate = true (页面也是)时,gridview 在代码中绑定到数据源。在回发期间,绑定到 gridview 的数据将丢失。可能是什么原因。请问是否需要代码。

回答by AGuyCalledGerald

Solved it, problem was I made a Page.Databind() in the Page_Load event of the masterpage of the page with the gridview, so it bound the gridview during each postback without data. Thanks for all efforts.

解决了它,问题是我在带有gridview的页面母版页的Page_Load事件中创建了一个Page.Databind(),因此它在每次没有数据的回发期间绑定了gridview。感谢所有的努力。

回答by Joel Etherton

This is by design. This data is not stored anywhere natively from page load to page load. You will need to perform one of the following 3 tasks:

这是设计使然。从页面加载到页面加载,这些数据不会本地存储在任何地方。您将需要执行以下 3 项任务之一:

  1. Store the data in ViewState (not necessarily recommended if the data is large)
  2. Store the data in a Session object (same story, large data equals bad memory usage)
  3. Make a return trip to rebind the data each time the page loads (breaks down if there is too much activity on the database or if the query is slow)
  1. 将数据存储在ViewState中(如果数据量大,不一定推荐)
  2. 将数据存储在 Session 对象中(同样的故事,大数据等于糟糕的内存使用)
  3. 每次页面加载时回程重新绑定数据(如果数据库活动过多或查询速度慢,则会中断)

My preference is to make return trips to the database when I need to and keep my SQL tuned for performance. Heavy page loads are annoying, and too much session memory can cause slowing on the server. I believe you can also store this data in cache, but I've never attempted it so I don't know what the limitations or capabilities there are.

我的偏好是在需要时返回数据库并保持 SQL 调整以提高性能。繁重的页面加载很烦人,过多的会话内存会导致服务器变慢。我相信您也可以将这些数据存储在缓存中,但我从未尝试过,所以我不知道有哪些限制或功能。

回答by Exoas

from the description of the problem. it sounds as if you are doing the databinding in the code behind. in such case asp.net does not preserve the datasource in the viewstate for you. try retrieving the data and storing it in the ViewState hashtable object with something like ViewState["GridviewData"] = GridviewData

从问题描述来看。听起来好像您正在后面的代码中进行数据绑定。在这种情况下,asp.net 不会为您保留视图状态中的数据源。尝试检索数据并将其存储在 ViewState 哈希表对象中ViewState["GridviewData"] = GridviewData

and retreiving it from there between postbacks

并在回发之间从那里检索它

回答by Bonshington

You are missing the concept here. This a very elementary question which google can answer for you.

你在这里错过了这个概念。这是一个非常基本的问题,谷歌可以为您解答。

Anyway, the thing is 'DataSource' of all DataBound control will behave as read-only and it will read when it needs to bind the control. To keep data existing you need some other way. Eg ViewState or Session

无论如何,所有 DataBound 控件的“DataSource”都将表现为只读,并且在需要绑定控件时会读取。为了保持数据存在,您需要一些其他方式。例如 ViewState 或 Session

  • ViewState is like a pocket embed to page. Bigger will make page load slower if it's bigger. The objects you store will be serialized to string based data. Bigger object cause a larger viewstate and greater bandwidth. Keep in mind that object has to he serializable.

  • Session is like a pocket for each user and it stays on server, means faster than viewstate. But big session will consume more MB in RAM

  • ViewState 就像一个嵌入页面的口袋。如果较大,则较大会使页面加载速度变慢。您存储的对象将被序列化为基于字符串的数据。更大的对象会导致更大的视图状态和更大的带宽。请记住,对象必须是可序列化的。

  • Session 就像每个用户的口袋,它留在服务器上,意味着比 viewstate 更快。但是大会话会在 RAM 中消耗更多 MB

edit noooo u still got it wrong. Enable ViewState doesn't help. let's take a look at structure.

编辑 noooo 你还是弄错了。启用 ViewState 没有帮助。让我们来看看结构。

<html>
 <head>  
 </head>
 <body>
  <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="..." />
 </body>
</html>

the input is an (actual)ViewState. When u set enable ViewState to WebControls it will store 'some serializable(mostly value type)' data. In your case, GridView.EnableViewState means it will store some property in it's own (logical)ViewState not page ViewState.

输入是(实际)ViewState。当您将启用 ViewState 设置为 WebControls 时,它将存储“一些可序列化(主要是值类型)”数据。在您的情况下,GridView.EnableViewState 意味着它将在它自己的(逻辑)ViewState 中存储一些属性而不是页面 ViewState。

In code behind this.ViewState["someName"] = ...; is page ViewState. Let's take a look in server side code.

在 this.ViewState["someName"] = ...; 后面的代码中 是页面视图状态。让我们看一下服务器端代码。

this.ViewState["someName"] = somDataTable; // this won't work
this.ViewState["someID"] = "abc"; // this work fine

Because DataTable doesn't has ISerializable which viewstate needs to convert any object to string base. U could implement your own serilization and assign to viewstate eg convert DataTable to byte[] and assign to view state.

因为 DataTable 没有 ISerializable 视图状态需要将任何对象转换为字符串基。你可以实现自己的序列化并分配给视图状态,例如将 DataTable 转换为 byte[] 并分配给视图状态。

try Session instead

改为尝试会话

回答by Kevin P

Ok I know the reply is really late, but I just encountered the same problem.

好的,我知道回复真的很晚,但我刚刚遇到了同样的问题。

I was Databinding everytime I loaded the page, and when there was a post back the gridview requeried. But using:

每次加载页面时我都在进行数据绑定,当有回发时,网格视图会重新查询。但是使用:

if(!IsPostBack)
{ //code used to bind on first page load }

in the Page_Load event seemed to have helped :)

在 Page_Load 事件中似乎有帮助:)