如何根据值动态更改转发器表TD颜色 [英] How to change repeater table td color dynamically based on value

查看:99
本文介绍了如何根据值动态更改转发器表TD颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何基于ASP.NET中该行的数据绑定值在中继器中设置表td颜色?

How can I set a table td color in my repeater based on the values databound to that row in ASP.NET?

推荐答案

使用ID的td,循环浏览转发器控件中的项目,然后使用FindControl(< id>")找到相应的控件并分配背景颜色属性或更改CSS.

如果以上方法均无效,请选择
您可以在td内创建一个Label控件,然后将文本绑定到该控件.现在尝试查找Label控件并更改其颜色.

You can assign the controls in the td with an ID, Loop through the items in the repeater control and then use FindControl("<id>") to find the respective control and assign the background color property or change the css.

If the above didn''t work, as an alternative
You can create a Label control inside the td and bind the text to it. Now try finding the Label control and change its color.

<td>
 <asp:Label ID="lbl1" runat="server" Text= <%#Container.DataItem("title")%>>  </asp:Label>
</td>





 foreach (RepeaterItem item2 in Repeater1.Items) 
{ 
        if (Convert.ToInt32(dr["Quantity"].ToString()) > 1)
        { 
            Label lbl= (Label)item2.FindControl("lbl1");
            lbl.Attributes.Add("style", "background-color:Green;"); 
        } 
}




请参见下面的链接.可能会对您有帮助.
在ItemDataBound事件上,您可以找到td,并可以根据所需条件设置颜色.

Hi,

Please see the below link. It might be help you.
On ItemDataBound Event you can find the td, and you can set the color as per your required condition .

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HtmlTableCell td = (HtmlTableCell)e.Item.FindControl("TD1"); //Where TD1 is the ID of the Table Cell
            if ("YOUR CONDITION")
            {
                td.Attributes.Add("style", "background-color:Green;");
            }
            else
            {
                td.Attributes.Add("style", "background-color:Yellow;");
            }
         }
}







http://www.asp.net/web-forms/tutorials/data-access/displaying-data-with-the-datalist-and-repeater/formatting-the-datalist-and-repeater-based-upon-data-vb[^]


这篇关于如何根据值动态更改转发器表TD颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆