嵌套在 GridView 中的数据绑定转发器不会更新
我有一个嵌套在 GridView
内的 Repeater
。在 GridView
的 RowDataBound
事件上,我设置了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最终通过完全删除
ViewState
的使用来解决这个问题,尽管我认为我的临时DataSource
会在postback
过程中丢失。 't。我最终选择了一个完美运行的类级变量。看来我没有正确理解回发
期间发生的情况。I ended up resolving the question by cutting out use of the
ViewState
entirely, though I thought my temporaryDataSource
would be lost across thepostback
it wasn't. I ended up going with a class-levelvariable
which works perfectly. It seems I didn't properly understand what happens during apostback
.首先,您不应该将数据源存储在 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.
我认为问题是您没有重新绑定
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 theViewState
object but are you actually triggering theBind
to occur? It sounds like you are not and the page is just reloading with the current data stored with the control'sViewState
.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 theRepeater
.