简体   繁体   English

以编程方式更改C#数据列表项

[英]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? 有什么例子吗?

The DataList has an ItemDataBound event which signals the addition of each item in the list. DataList有一个ItemDataBound事件,该事件表示列表中每个项目的添加。 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 您可以在MSDN库中找到有关事件和参数的特定详细信息

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. 另一种方法是使用ItemDataBound事件。 Here in MSDN you can see an example. 在MSDN中,您可以看到一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM