ASP.NET page events - Button click event comes after GridView bind
Solution 1
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.
Solution 2
ASP.NET does, by default, lots of binding and rebinding. Rebinding after a click event is normal.
TallGuy
Software developer located in Wellington, New Zealand. Occasional movie star (we make a few around here). Tall.
Updated on June 14, 2022Comments
-
TallGuy over 1 year
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?
-
Jon Adams over 14 yearsI got no rep yet either, not compared to ocdecio anyway! :)