ASP.NET 页面事件 - GridView 绑定后发生按钮单击事件
我对页面事件顺序的理解是这样的:
页面:加载
Control:DataBind(用于 GridView 或其他)
控制:加载
控制:单击(对于按钮)
页面:预渲染
控制:预渲染
(还有很多其他控件 - 但这些是我感兴趣的
)这里需要注意的是,按钮的单击事件发生在 gridview 的绑定事件之后。 如果该按钮导致数据发生更改,则 GridView 将显示旧数据。 我可以在 PreRender 事件中重新绑定控件,但这看起来非常丑陋。
这一定是一个非常常见的模式(更新数据的按钮)。 如何将它们放在一起,以便 GridView 在按钮单击更改数据后绑定到数据?
My understanding of the order of page events is this:
Page : Load
Control : DataBind (for a GridView or whatever)
Control : Load
Control : Clicked (for a Button)
Page: PreRender
Control : PreRender
(There are lots of others - but these are the ones I'm interested in)
The important thing to notice here is that the button's click event comes after the gridview's bind event. If the button causes a change to the data, the GridView displays the old data. I could rebind the control in the PreRender event, but that seems totally ugly.
This must be a very common pattern (a button that updates data). How do I put this together so that the GridView binds to the data after the Button click changes it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
按钮单击事件发生在 GridView 绑定之后的唯一原因是您对页面进行了编程以执行此操作。 我没有看到在 PreRender 事件中绑定控件有任何问题,事实上,这是在控件事件(例如 Button onclick)之后采取操作的唯一方法。
The only reason the button click event comes after GridView bind is because you programmed your page to do that . I don't see any problem in binding the control in the PreRender event, in fact that is the only way to take action after a control event (such as Button onclick).
答案是在 Button Click 事件中,在数据更改后,调用页面上的 DataBind() 以使 GridView(以及任何其他需要它的东西)重新绑定。 我没想到你能做到这一点。
谢谢奥德西奥和 Mufasa - 我会将您的答案标记为有帮助,但我还没有得到代表。
The answer was in the Button Click event, after the data has been changed, call DataBind() on the page to cause the GridView (and anything else that needs it) to rebind. I didn't realise you could do that.
Thank you Ocdecio & Mufasa - I would mark your answers as helpful, but I got no rep yet.
默认情况下,ASP.NET 会进行大量绑定和重新绑定。 点击事件后重新绑定是正常的。
ASP.NET does, by default, lots of binding and rebinding. Rebinding after a click event is normal.