Yup, it'll work that way. :-) (I've used the same technique). Just to add technical detail that e.Item.DataItem is also non-null in ItemCreated when DataBind is called because on databind items will be recreated and both ItemCreated and ItemDataBound will be
called. On ItemCreated the created DataGridItem just isn't yet added to Items/Controls so its lifecycle hasn't started yet (it's sort of initializing method for stuff to be added to the current item) However, idea of the sequence is that if you need to do
something (like create dynamical controls) based on data, you'd have the creation logic tied to ItemDataBound and recreation for postback in ItemCreated (means using some sort of flags to indicate from ItemDataBound to ItemCreated that something should be
recreated). As an example I've developed a datagrid where type of the control created varies row by row basis, so change from one input box to a editable DropDownList is needed and this is a way to have such solution. There's a row-level flag (has row index
as the main key) on ViewState which tells the type of control to be created and it is set on ItemDataBound (based on data), and of course in ItemCreated it is re-read and correct type of control is recreated based on it.
joteke
All-Star
46224 Points
6896 Posts
ASPInsiders
MVP
Re: Dynamicall Created Controls in a Datagrid
Nov 12, 2004 07:47 PM|LINK
Teemu Keiski
Finland, EU