views:

229

answers:

1

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.

+1  A: 

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.

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

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;
        }
    }
}
Claudio Redi
Hey Claudio, it's still not working correctly. When I tried your suggestion, the page loaded without error, but the label that has Text set to True did not show the backcolor. Thank you.
Doug
@Doug. Please try to execute this same code on event OnItemDataBound
Claudio Redi
YES! Now it's working. Should have used OnItemDataBound in the first place. Thank you Claudio.
Doug