This is to expound more on my previous post here regarding the behavior of datagrid's events.
I tried to refactor my codes one time on a webform with a grid on it. Most of my ItemCommands has similar routine, obtain the PK id of the record from the grid’s datakey collection, instantiate the corresponding object and store it to Session. I tried to remove the said routine outside my switch block:
[code language="C#"]
int pkID = datagrid1.DataKeys[e.Item.ItemIndex];
MyObject myObject = new MyObject(pkID);
switch(e.CommandName)
{
...
}
[/code]
Obviously the above handler for the grid’s ItemCommand event will raise an exception when the value of e.Item.ItemIndex is -1 which happens for special grid events like PageIndexChanged (CommandName = “Page”) and SortCommand (CommandName = “Sort”) which are special commands of the grid which triggers ItemCommand event first before their corresponding events.
Posted
Aug 04 2005, 11:24 PM
by
jokiz