How to find the one Label in DataList that is set to True
- by Doug
In my .aspx page I have my 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. (Note:I'm using a recursive findControl class)
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. But it sets the itemLabel color to the first instance of the itemLabel found in the DataList. 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. Also: The itemLabel is picking up a column in the DB called "CollectionHomePage" which is True/False bit data type. I must be missing something simple... Thanks for your ideas.