Aaaaaaaaaargh! It has happened again. I continually get problems with DataGrids not firing their PageIndexChanged event. I’ve had this issue pop up at least 10 times in the last month and I still can’t explain it although I have a sort of solution which I’ll share at the end. Sort of because it works, but sort of because I have no idea why it does <g>…

 

Here’s what happens: I create a new datagrid, which in many cases is Cut and Pasted from somewhere else in the application to basically ‘inherit’ the visual layout. I then go in and customize the data setup for the grid.

 

Lastly I add the paging and add Paging support.

 

<asp:datagrid id="dgErrorLog" runat="server"
onpageindexchanged="dgErrorLog_OnPageIndexChanged"
              enableviewstate="False" allowpaging="True" pagesize="20"
              autogeneratecolumns="False" width="100%" >


Nothing that I haven't done a million times before, but on this page the event in the codebehind is not firing:

protected void dgErrorLog_OnPageIndexChanged(object sender,

      DataGridPageChangedEventArgs e)

{

      this.dgErrorLog.CurrentPageIndex = e.NewPageIndex;

      this.ShowLog(); // *** does a Databind()

}


All other events in the page are firing just fine and the EventTarget is posting to the page just fine. But the event code just doesn't fire. I also removed the above Attribute based eventhandler and instead hooked the event to the control through the designer with the explicit event handler generated in the InitializeComponent section. Same result – the event hookup code is run (I can see it in the debugger), but the event is never firing.

 

Checking the Trace output I can see that clicking on the Paging button properly fires the JavaScript handler that posts back the EventID which shows up on the server. Still no event…

As mentioned this has happened to me a few times before. And I’ve been able to get this stuff to work once again through what I consider to be black magic: Delete the DataGrid in the designer. Save and close the form. Re-open the form. Add a new DataGrid name it and save. Then recopy the detail of the grid back in and then re-attach the event. Lo and behold it works and my events fire again.

 

But what in the heck is going on here? What could possibly be causing the even to not fire with an event handler getting hooked up. And what in the process of deleting the control altogether would cause it to start working? As far as I understand there’s nothing in the ASP. Net form like resources that have any hooks to the controls on the form…

 

Anybody have any ideas on why this is happening?

 

As an aside here’s another gotcha that is related and has bitten me before. If you change the PageIndex you need to re-bind the grid after changing the index. In order for the event to fire you also need to make sure you bind before the event. In other words, if you change pages you must DataBind() the grid twice to get the event to fire and properly display the result. Talk about non-intuitive – it’s easy to overlook this little detail.