In my webpage, I have a gridview which is created in the Page_Load method that retrieves some values from a database table. I also have a button that when clicked, causes an event handler which adds/deletes rows from the table. Currently, when I press the button, the event handler runs but the gridview is not updated until another postback occurs.
To me, it seems like this means that the postback occurs before the event handler is executed. Is this correct? If so, how can I make it so that the button press updates the table and then reloads the gridview? I suppose I could recreate the gridview datasource in the event handler but that seems like a very inefficient way of approaching the problem.
That is correct.
Control Eventsare handled after thePage Loadevent.Review this documentation about the
Page Lifecyclefrom MSDN.What could be helpful for you to understand this is to set breakpoints in your code on the
Page_Loadmethod and yourButton Clickevent handler, then click the button and see when each breakpoint is hit.For your
Gridview, you’ll have to call your binding code again in order for your changes to show after the postback. The best way to do that would be to create a method with the binding code, and call it from the event handler.