嵌套在 GridView 中的数据绑定转发器不会更新

发布于 2024-12-12 14:55:08 字数 669 浏览 5 评论 0原文

我有一个嵌套在 GridView 内的 Repeater。在 GridViewRowDataBound 事件上,我设置了 DataSource (基于行的列之一),然后绑定 Repeater。这对于初始加载来说效果很好,但是我需要能够动态地将新项目添加到 Repeater 中。

我将一个项目附加到 DataSource 中,将其保存到 ViewState 中,在通常使用方法调用进行绑定的位置,我绑定到保存到 中的对象相反,ViewState。 DataSouce 反映了更改,但页面却没有。

我缺少什么?我在另一页上有完全相同的设置,没有嵌套,并且效果很好。

if (ViewState["RepeaterObj"]!=null)
{
   rpt.DataSource=(IList<DataTransferObject>)ViewState["RepeaterObj"];
}
else
{
   rpt.DataSource = controller.GetObj(param);
   rpt.DataBind();
}

I have a Repeater nested inside of a GridView. On the RowDataBound event for the GridView, I set the DataSource (based on one of the row's columns) and then bind the Repeater. This works fine for the initial load, however I need to be able to add new items to the Repeater dynamically.

I append an item to the DataSource, save it to the ViewState, and where I would normally bind using a method call, I bind to the object saved to the ViewState instead. The DataSouce reflects the change, however the page does not.

What am I missing? I have the exact same setup on another page without the nesting and it works perfectly.

if (ViewState["RepeaterObj"]!=null)
{
   rpt.DataSource=(IList<DataTransferObject>)ViewState["RepeaterObj"];
}
else
{
   rpt.DataSource = controller.GetObj(param);
   rpt.DataBind();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏日浅笑〃 2024-12-19 14:55:08

我最终通过完全删除 ViewState 的使用来解决这个问题,尽管我认为我的临时 DataSource 会在 postback 过程中丢失。 't。我最终选择了一个完美运行的类级变量。看来我没有正确理解回发期间发生的情况。

I ended up resolving the question by cutting out use of the ViewState entirely, though I thought my temporary DataSource would be lost across the postback it wasn't. I ended up going with a class-level variable which works perfectly. It seems I didn't properly understand what happens during a postback.

七秒鱼° 2024-12-19 14:55:08

首先,您不应该将数据源存储在 ViewState 中。这几乎是你能把它放在最糟糕的地方。

至于你的问题,我建议要么在新项目添加到中继器时重新绑定 GridView,要么在保存新记录的事件中找到中继器并将其重新绑定在那里。

First of all, you shouldn't be storing a datasource in ViewState. That's pretty much the worst place you could put it.

As for your problem, I would suggest either rebinding the GridView when new items are added to the repeater, or find the repeater in the event that saves the new record and rebind it there.

梦回梦里 2024-12-19 14:55:08

我认为问题是您没有重新绑定Repeater。您说您更改了绑定方式以查看 ViewState 对象,但您实际上是否触发了 Bind 发生?听起来好像您没有,页面只是使用控件的 ViewState 存储的当前数据重新加载。

确保您显式调用 Repeaters 绑定事件以确保它得到反弹。

编辑:我怀疑它可能与您可能需要重新绑定 GridView 而不仅仅是 Repeater 的情况有关。

I think the problem is you are not rebinding the the Repeater. You say you change how you bind to look at the ViewState object but are you actually triggering the Bind to occur? It sounds like you are not and the page is just reloading with the current data stored with the control's ViewState.

Make sure you are calling your Repeaters bind event explicitly to sure it is getting rebound.

EDIT: I suspect it might have something to do where you might need to rebind your GridView and not just the Repeater.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文