C# – Changing C# datalist item programmatically

asp.netc++data-bindingdatalist

I have a datalist i want to programmatically run some checks and then change the text that is been displayed. Can this be done ? Any examples?

Best Solution

The DataList has an ItemDataBound event which signals the addition of each item in the list. By subscribing to this event can process each item data being added.

Server control:

<asp:DataList id="ItemsList"
       ...
       OnItemDataBound="ItemDataBound"
       runat="server">

Code behind:

protected void ItemDataBound(Object sender, DataListItemEventArgs e)
{
   if (e.Item.ItemType == ListItemType.Item || 
       e.Item.ItemType == ListItemType.AlternatingItem)
   {
       //process item data
   }
}

You can find specific details about the event and parameters in the MSDN Library