none
设置Repeater中指定项的颜色,请问如何简化这段代码? RRS feed

  • 问题

  • 功能是根据指定的 EmployeeId条件设置该项显示为红色,我在这里使用了2个 PlaceHolder 。请问还有更好的方式吗
    代码如下:
     protected void EmployeesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem != null)
            {
                System.Data.DataRowView drv = e.Item.DataItem as System.Data.DataRowView;
                if (drv["EmployeeId"].ToString() == "1" || drv["EmployeeId"].ToString() == "5")
                {
                    PlaceHolder red = e.Item.FindControl("RedPlaceHolder") as PlaceHolder;
                    PlaceHolder general = e.Item.FindControl("GeneralPlaceHolder") as PlaceHolder;
                    red.Visible = true;
                    general.Visible = false;
                }

            }
        }

    <ItemTemplate>
                    <tr>
                        <td>
                            <asp:PlaceHolder ID="RedPlaceHolder" Visible="false" runat="server">
                                <span class="red" > <%#Eval("EmployeeId")%></span>
                            </asp:PlaceHolder>
                            <asp:PlaceHolder ID="GeneralPlaceHolder" runat="server">
                                <%#Eval("EmployeeId")%>
                            </asp:PlaceHolder>
                        </td>
                        <td>
                            <%#Eval("firstName")%>
                        </td>
                        <td>
                            <%#Eval("lastName")%>
                        </td>
                    </tr>
                </ItemTemplate>

    拒绝垃圾代码
    2009年11月25日 15:57

答案

  •      <asp:Repeater ID="Repeater1" runat="server"
                onitemdatabound="Repeater1_ItemDataBound" DataSourceID="SqlDataSource1">
           <ItemTemplate>
           <asp:Label runat="server" Text='<%# Eval("id") %>'></asp:Label>
           </ItemTemplate>
            </asp:Repeater>
          
            protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    if (e.Item.HasControls())
                    {
                        Label s = e.Item.Controls[1] as Label;
                        if (s.Text == "1" || s.Text == "2")
                        {
                            s.CssClass = "red";
                        }
                    }
                }
    努力学习wpf
    2009年11月26日 7:21

全部回复

  • 我还想到了一种写法,但是这样需要在CS代码中拼接HTML字符串所以感觉也不是很好,请老师指点

        protected void EmployeesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem != null)
            {
                System.Data.DataRowView drv = e.Item.DataItem as System.Data.DataRowView;
                if (drv["EmployeeId"].ToString() == "1" || drv["EmployeeId"].ToString() == "5")
                {
                    Literal employeeIdLiteral = e.Item.FindControl("EmployeeIdLiteral") as Literal;
                    //HtmlGenericControl span = new HtmlGenericControl("span");
                    //span.Attributes.Add("class", "red");
                    employeeIdLiteral.Text = "<span class=\"red\">" + drv["EmployeeId"].ToString() + "</span>";
                }

            }
        }

                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:Literal ID="EmployeeIdLiteral" Text='<%#Eval("EmployeeId")%>' runat="server"></asp:Literal>
                        </td>
                       
                        <td>
                            <%#Eval("firstName")%>
                        </td>
                        <td>
                            <%#Eval("lastName")%>
                        </td>
                    </tr>
                </ItemTemplate>

    2009年11月25日 16:17
  •             if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                       System.Data.DataRowView drv = e.Item.DataItem as System.Data.DataRowView;
                if (drv["EmployeeId"].ToString() == "1" || drv["EmployeeId"].ToString() == "5")
                {
                      ( e.Item.FindControl("Label1")as Label).CssClass="red";
                }
    
    
    <asp:Label ID="Label1" runat="server" Text='<%#Eval("EmployeeId")%>'></asp:Label>
            

    努力学习wpf
    2009年11月25日 16:47
  •     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                     Label s=  e.Item.FindControl("Label1")as Label;
                    if (s.Text == "1" ||s.Text== "5")
                {
                    s.CssClass="red";
                }

    努力学习wpf
    2009年11月25日 16:51
  • 谢谢回复,
    这样写的话Label1会生成较长的客户端ID,请问还有其他方法吗?
    拒绝垃圾代码
    2009年11月26日 0:35
  • 我还想到了一种写法,但是这样需要在CS代码中拼接HTML字符串所以感觉也不是很好,请老师指点

        protected void EmployeesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem != null)
            {
                System.Data.DataRowView drv = e.Item.DataItem as System.Data.DataRowView;
                if (drv["EmployeeId"].ToString() == "1" || drv["EmployeeId"].ToString() == "5")
                {
                    Literal employeeIdLiteral = e.Item.FindControl("EmployeeIdLiteral") as Literal;
                    //HtmlGenericControl span = new HtmlGenericControl("span");
                    //span.Attributes.Add("class", "red");
                    employeeIdLiteral.Text = "<span class=\"red\">" + drv["EmployeeId"].ToString() + "</span>";
                }

            }
        }

                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:Literal ID="EmployeeIdLiteral" Text='<%#Eval("EmployeeId")%>' runat="server"></asp:Literal>
                        </td>
                       
                        <td>
                            <%#Eval("firstName")%>
                        </td>
                        <td>
                            <%#Eval("lastName")%>
                        </td>
                    </tr>
                </ItemTemplate>

    这种方法就挻好的。
    知识改变命运,奋斗成就人生!
    2009年11月26日 1:57
    版主
  • 谢谢回复,
    这样写的话Label1会生成较长的客户端ID,请问还有其他方法吗?
    拒绝垃圾代码

    mldark的方法已经很简单了,
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                     Label s=  e.Item.FindControl("Label1")as Label;
                    if (s.Text == "1" ||s.Text== "5")
                {
                    s.CssClass="red";
                }
    

    如果你真的要实现在客户端没有Id的span,那么就可以自定义一个Label控件,以满足你的功能,但真的没有必要!
    努力!奋斗
    2009年11月26日 2:59
  • 自定义控件代码:

    using System;
    using System.Text;
    
    using System.ComponentModel;
    using System.Web;
    using System.Web.UI;
    
    namespace SimpleCC
    {
        [ToolboxData("<{0}:SuperLabel runat=\"server\"></{0}:SuperLabel>")]
        public class SuperLabel : Control
        {
            [Browsable(true)]
            [Category("generic")]
            [DefaultValue("")]
            [Description("文本")]
            public virtual string Text
            {
                get
                {
                    return ViewState["Text"] == null ? string.Empty : (string)ViewState["Text"];
                }
                set
                {
                    ViewState["Text"] = value;
                }
            }
    
            /// <summary>
            /// 呈现具体的内容
            /// </summary>
            /// <param name="writer"></param>
            protected override void Render(HtmlTextWriter writer)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "red");     //给下面的Span添加样式属性
                writer.RenderBeginTag(HtmlTextWriterTag.Span);                  //呈现开始标记
                writer.Write(this.Text);
                writer.RenderEndTag();                                          //呈现结束标记
                base.Render(writer);
            }
        }
    }
    


    测试页面代码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SuperLabelTest.aspx.cs" Inherits="SuperLabelTest" %>
    
    <%@ Register Assembly="SimpleCC" Namespace="SimpleCC" TagPrefix="cc1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>SuperLabel测试</title>
        <style type="text/css">
            .red
            {
            	color:Red;
            	font-size:20px;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="rpList" runat="server" OnItemDataBound="rpList_ItemDataBound">
                <ItemTemplate>
                    <asp:Literal ID="ltShow" runat="server"></asp:Literal>
                    <cc1:SuperLabel ID="slShow" runat="server" Visible="false"/>
                </ItemTemplate>
            </asp:Repeater>
        </div>
        </form>
    </body>
    </html>
    后台代码:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    using SimpleCC;
    
    public partial class SuperLabelTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                List<string> list = new List<string>() { "aaa","1","bbb","5"};
                this.rpList.DataSource = list;
                this.rpList.DataBind();
            }
        }
    
        /// <summary>
        /// 项绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rpList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string result = e.Item.DataItem.ToString();
                Literal ltShow = e.Item.FindControl("ltShow") as Literal;
                SuperLabel slShow = e.Item.FindControl("slShow") as SuperLabel;
                if (result == "1" || result == "5")
                {
                    ltShow.Visible = false;
                    slShow.Visible = true;
                    slShow.Text = result;
                }
                else
                {
                    ltShow.Text = result;
                }
            }
        }
    }

    努力!奋斗
    2009年11月26日 3:32
  •      <asp:Repeater ID="Repeater1" runat="server"
                onitemdatabound="Repeater1_ItemDataBound" DataSourceID="SqlDataSource1">
           <ItemTemplate>
           <asp:Label runat="server" Text='<%# Eval("id") %>'></asp:Label>
           </ItemTemplate>
            </asp:Repeater>
          
            protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    if (e.Item.HasControls())
                    {
                        Label s = e.Item.Controls[1] as Label;
                        if (s.Text == "1" || s.Text == "2")
                        {
                            s.CssClass = "red";
                        }
                    }
                }
    努力学习wpf
    2009年11月26日 7:21
  •      <asp:Repeater ID="Repeater1" runat="server"
                onitemdatabound="Repeater1_ItemDataBound" DataSourceID="SqlDataSource1">
           <ItemTemplate>
           <asp:Label runat="server" Text='<%# Eval("id") %>'></asp:Label>
           </ItemTemplate>
            </asp:Repeater>
          
            protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    if (e.Item.HasControls())
                    {
                        Label s = e.Item.Controls[1] as Label;
                        if (s.Text == "1" || s.Text == "2")
                        {
                            s.CssClass = "red";
                        }
                    }
                }
    努力学习wpf

    我个人觉得这种方法不太适合,因为
    <asp:Label runat="server" Text='<%# Eval("id") %>'></asp:Label>
    在e.Item.Controls集合中的索引有时候可能是1,有时候却不一定是1,不好控制,还是根据Id来查找放心一些。


    努力!奋斗
    2009年11月26日 9:04
  •      <asp:Repeater ID="Repeater1" runat="server"
                onitemdatabound="Repeater1_ItemDataBound" DataSourceID="SqlDataSource1">
           <ItemTemplate>
           <asp:Label runat="server" Text='<%# Eval("id") %>'></asp:Label>
           </ItemTemplate>
            </asp:Repeater>
          
            protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    if (e.Item.HasControls())
                    {
                        Label s = e.Item.Controls[1] as Label;
                        if (s.Text == "1" || s.Text == "2")
                        {
                            s.CssClass = "red";
                        }
                    }
                }
    努力学习wpf

    我个人觉得这种方法不太适合,因为
    <asp:Label runat="server" Text='<%# Eval("id") %>'></asp:Label>
    在e.Item.Controls集合中的索引有时候可能是1,有时候却不一定是1,不好控制,还是根据Id来查找放心一些。


    努力!奋斗

    没有完美方法   lz的需求 我只能满足需求  确实不好控制  可以多测试一下 就行了 看控件从上到下顺序可以找到素引
    努力学习wpf
    2009年11月26日 9:09
  • 谢谢各位了
    拒绝垃圾代码
    2009年11月26日 9:53