Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 766
TallGuy Avatar asked Feb 16 '09 01:02

TallGuy


2 Answers

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.

like image 125
TallGuy Avatar answered Sep 20 '22 09:09

TallGuy


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

like image 33
Jon Adams Avatar answered Sep 22 '22 09:09

Jon Adams