Thursday, May 1, 2008

DataGrid.ItemCommand doesn't fire

In one of my web application, ItemCommand event of a datagrid doesn't fire as expected. After some work, I find out the reason.

The bind to datasouce code of this datagrid control is put in Page_PreRender handler, not in Page_Load. On first load of the page, everything is fine, the rows are populated in datagrid and I add "cancel" button for each row.

Then I click "cancel" button, which trigger a postback. Again, Page_Load hander runs first. Then .NET framework try to call ItemCommand event, I reckon it checks the rows in datagrid and since their are no rows yet. It decides not to call ItemCommand. Then Page_PreRender is called. The command is ignored.

So, don't do important tasks in Page_PreRender

No comments: