Accessing the current DataItem in the ItemCommand event

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

I need to access one of the columns from the current DataItem in the
ItemCommand event, which I call using EventBubbling with a DataList and
Button control. The code I tried to use to access the current DataItem is
the following:

CStr(CType(e.Item.DataItem, DataRowView)("id"))


However, after some experimenting, I found out that e.Item.DataItem is
Nothing in the ItemCommand event by using the following statement:

System.Diagnostics.Debug.WriteLine("IsNothing(e.Item.DataItem): " &
CStr(IsNothing(e.Item.DataItem)))


This statement ouput the value "True". Does anybody have any ideas as to
what might be a good way to get the column values that were used for the
current Item? Thanks.
 
DataItem is available only in ItemDataBound event as it is the only time
where the control items connect to their datasource. If you need the
datasource field values in other places, you need to take a special care of
it. You may consider including the values in the item template and hiding
them with css style display:none. Then you can access them in postbacks.
 
Back
Top