简体   繁体   中英

How to find the one Label in DataList that is set to True

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.

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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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