开发者

Benefit of Binding a GridView in Page_PreRender vs Page_Load?

开发者 https://www.devze.com 2022-12-19 10:55 出处:网络
Assume I have an asp:GridView with some customer information.On that page I also have a button which allows me to add a new user to the GridView.When I add the click the user, I databind the gridview

Assume I have an asp:GridView with some customer information. On that page I also have a button which allows me to add a new user to the GridView. When I add the click the user, I databind the gridview again in the button click. Correct me if I am wrong, but if the asp.net lifecycle or at least part of it is in the following order:

page_load Hookup event handlers such as my button click page_preRender

Does this mean that if I put the databinding for the gridview in preRender, that is the only place I need to worry about calling it. 开发者_Go百科 I can remove it from the button click. This is what I think, but I am not sure if my thinking is correct, so I would like some more insight as to the benefit of putting code in PreRender as opposed to PageLoad


By PreRender, you assume that most logic in the page that would affect the binding result has been completed (usually in Page_Load, but anywhere earlier really).

Controls in the .Net framework by default do their binding in the PreRender event (makes sense, bind to the datasource at the last possible moment...could be that you changed the data 400 times earlier in the lifecycle). Here's the full layout of the 2.0 lifecycle for reference.

PreRender - Before this event occurs:

  • The Page object calls EnsureChildControls for each control and for the page.
  • Each data bound control whose DataSourceID property is set calls its DataBind method. For more information, see Data Binding Events for Data-Bound Controls later in this topic.

The PreRender event occurs for each control on the page. Use the event to make final changes to the contents of the page or its controls.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号