ASP.NET page events - Button click event comes after GridView bind
Asked Answered
D

3

6

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?

Disquietude answered 16/2, 2009 at 1:30 Comment(0)
D
2

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.

Disquietude answered 16/2, 2009 at 4:8 Comment(1)
I got no rep yet either, not compared to ocdecio anyway! :)Karat
K
1

ASP.NET does, by default, lots of binding and rebinding. Rebinding after a click event is normal.

Karat answered 16/2, 2009 at 2:6 Comment(0)
M
0

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).

Maroney answered 16/2, 2009 at 1:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.