[英]How to find the one Label in DataList that is set to True
In my .aspx page I have my DataList: 在我的.aspx页中,有我的DataList:
<asp:DataList ID="DataList1" runat="server" DataKeyField="ProductSID"
DataSourceID="SqlDataSource1" onitemcreated="DataList1_ItemCreated"
RepeatColumns="3" RepeatDirection="Horizontal" Width="1112px">
<ItemTemplate>
ProductSID:
<asp:Label ID="ProductSIDLabel" runat="server" Text='<%# Eval("ProductSID") %>' />
<br />
ProductSKU:
<asp:Label ID="ProductSKULabel" runat="server" Text='<%# Eval("ProductSKU") %>' />
<br />
ProductImage1:
<asp:Label ID="ProductImage1Label" runat="server" Text='<%# Eval("ProductImage1") %>' />
<br />
ShowLive:
<asp:Label ID="ShowLiveLabel" runat="server" Text='<%# Eval("ShowLive") %>' />
<br />
CollectionTypeID:
<asp:Label ID="CollectionTypeIDLabel" runat="server" Text='<%# Eval("CollectionTypeID") %>' />
<br />
CollectionHomePage:
<asp:Label ID="CollectionHomePageLabel" runat="server" Text='<%# Eval("CollectionHomePage") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
And in my code behind using the ItemCreated event to find and set the label.backcolor property. 在我后面的代码中,使用ItemCreated事件查找并设置label.backcolor属性。 ( Note:I'm using a recursive findControl class )
( 注意:我正在使用递归的findControl类 )
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label itemLabel = form1.FindControlR("CollectionHomePageLabel") as Label;
if (itemLabel !=null || itemLabel.Text == "True")
{
itemLabel.BackColor = System.Drawing.Color.Yellow;
}
}
When I run the page, the itemLabel is found, and the color shows. 当我运行页面时,找到itemLabel,并显示颜色。 But it sets the itemLabel color to the first instance of the itemLabel found in the DataList.
但是它将itemLabel颜色设置为在DataList中找到的itemLabel的第一个实例。 Of all the itemLabels in the DataList, only one will have it's text = True - and that should be the label picking up the backcolor.
在DataList中的所有itemLabel中,只有一个具有text = True-且应该是拾取背景色的标签。 Also: The itemLabel is picking up a column in the DB called "CollectionHomePage" which is True/False bit data type.
另外:itemLabel正在数据库中选择一个名为“ CollectionHomePage”的列,该列为True / False位数据类型。 I must be missing something simple... Thanks for your ideas.
我一定想念一些简单的东西...感谢您的想法。
ItemCreated
event is executed for each data list item, it's not global so you're executing the same code for EACH item and I'm afraid this is wrong on your case. ItemCreated
事件是针对每个数据列表项目执行的,它不是全局的,因此您正在为每个项目执行相同的代码,而且在您的情况下,这恐怕是错误的。
You need to check only the current item that it's been created. 您只需要检查当前已创建的项目。 Also, since on item created the data is not yet bound to the item you need to use the
ItemDataBound
event 另外,由于在创建的项目上数据尚未绑定到项目,因此您需要使用
ItemDataBound
事件
Here you have a snippet that may work for you 这里有一个片段可能适合您
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
foreach(Control control in e.Item.Controls)
{
if (control is Label && (control as Label).Text.Equals("True"))
{
(control as Label).BackColor = System.Drawing.Color.Yellow;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.