如何找到DataList中设置为True的一个Label

发布于 2024-09-03 10:12:53 字数 2161 浏览 4 评论 0原文

在我的 .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>

在我的代码中,使用 ItemCreated 事件查找并设置 label.backcolor 属性。 (注意:我正在使用递归 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;
          }
     }

当我运行页面时,会找到 itemLabel,并显示颜色。但它将 itemLabel 颜色设置为 DataList 中找到的 itemLabel 的第一个实例。在 DataList 中的所有 itemLabels 中,只有一个的 text = True - 这应该是选择背景色的标签。另外: itemLabel 在数据库中选取一个名为“CollectionHomePage”的列,它是 True/False 位数据类型。我一定错过了一些简单的东西......谢谢你的想法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃气十足 2024-09-10 10:12:53

ItemCreated 事件是为每个数据列表项执行的,它不是全局的,因此您为每个项目执行相同的代码,恐怕这对您的情况是错误的。

您只需检查已创建的当前项目。另外,由于创建的项目上的数据尚未绑定到您需要使用 ItemDataBound 事件的项目,

这里您有一个可能适合您的代码片段

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

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文