ferronrsmith
ferronrsmith

Reputation: 1110

Changing C# datalist item programmatically

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?

Upvotes: 0

Views: 5832

Answers (2)

Mircea Grelus
Mircea Grelus

Reputation: 2915

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

Upvotes: 2

Canavar
Canavar

Reputation: 48088

You can do your calculations and checks on datalist control's data source (datatable, collection, ... etc.). Also you can programmatically change the values of the items that datalist display by updating the datasource of the datalist.

An alternative way is using ItemDataBound event. Here in MSDN you can see an example.

Upvotes: 1

Related Questions