Changing C# datalist item programmatically

淺唱寂寞╮ 提交于 2019-12-11 18:41:19

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/1034915/changing-c-sharp-datalist-item-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!