重新排序后更新 GridView
我有一个依赖于三个级联下拉菜单(在更新面板中)的网格视图。当用户编辑网格视图中的单元格、更改下拉列表并单击“显示”时,它会调用以下隐藏代码:
protected void Save_Click(Object sender, EventArgs e)
{
foreach (GridViewRow gvr in MasterDisplay.Rows)
MasterDisplay.UpdateRow(gvr.RowIndex, false);
MasterDisplay.DataBind();
}
问题是下拉列表将更改网格视图,因此更新的单元格会乱序。举个例子:假设我有六个项目:A1、A2、A3、B1、B2、B3。如果我编辑 B2 的价格,然后更改显示“首先显示 B”的下拉菜单,在结果页面上我将看到 A2 的新价格(即订单中的第五个价格)。
我认为这是因为在 UpdateRow() 中我们仅基于索引进行更新 (gvr.RowIndex),因此 GridView 会以某种方式重新显示(不会丢失新的价格数据!),然后更新。
关于如何确保这些更改保存到适当的行,而不仅仅是现在恰好具有与以前相同的索引的行,有什么想法吗?
I have a gridview reliant on three cascading dropdowns (in an updatepanel). When the user edits a cell in the gridview, changes a dropdown, and clicks 'Display' it calls the following codebehind:
protected void Save_Click(Object sender, EventArgs e)
{
foreach (GridViewRow gvr in MasterDisplay.Rows)
MasterDisplay.UpdateRow(gvr.RowIndex, false);
MasterDisplay.DataBind();
}
The problem is that the dropdowns will change the gridview, so the updated cells get out of order. An example: Let's say I have six items: A1, A2, A3, B1, B2, B3. If I edit price for B2, then change the dropdown that says 'show Bs first', on the resulting page I'll see the new price for A2 instead (ie, whatever is fifth in the order).
I figure this is because in UpdateRow() we're only updating based on the index (gvr.RowIndex), so somehow the GridView is getting redisplayed (without losing the new price data!) and then updated.
Any ideas on how to ensure that these changes save to the appropriate row, rather than just the row that happens to now have the same index as previously?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,正如越来越普遍的那样,我想我会回答我自己的问题,以防有一天有人偶然发现这一点。 (即http://xkcd.com/979/)
基本上,我创建了三个标签,这些标签是从Page_Load 上的 Request.QueryString(变量在 url 中传递)。这些标签现在是构建 gridview 的 SqlDataSource 的 ControlParameters。我还填写了该变量的下拉列表。即
然后用户可以正常使用下拉菜单(从他们的一端)。当用户单击“保存”时,我运行当前代码(请参阅问题),然后添加:
这意味着不再有回发,而是导航。
一个重要的注意事项:我必须填写 if(!Page.IsPostBack) 中的所有下拉菜单,因为我搭载的预先存在的函数似乎被多次调用。不知道为什么他们被称为,因为他们只在一份声明中
(使用下拉菜单后被调用三次,即使外部 gridview 位于 updatepanel 外部)
So, as is becoming common, I guess I'll answer my own question in case one day someone stumbles upon this. (ie http://xkcd.com/979/)
Basically, I created three labels that are fed variables from Request.QueryString on Page_Load (where the variables are passed in the url). These labels are now the ControlParameters for the SqlDataSource building the gridview. I also fill the dropdowns from this variable. ie
Then the user can use dropdowns as normal (from their end). When the user clicks save, I run the current code (see question) and then add:
This means no more postbacks, but instead navigation.
One important note: I had to fill all dropdowns in if(!Page.IsPostBack) because the pre-existing functions I piggybacked on seemed to be getting called multiple times. Not sure why they were called since they were only in one declaration
( got called three times after dropdown was used, even though outside gridview is outside updatepanel)