zoukankan      html  css  js  c++  java
  • Repeater控件合并相同信息行及设定行背景颜色“类”

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    
    namespace xxxx
    {
        public class RepeaterSetting
        {
            /// <summary>
            /// 合并Repeater中某行相同信息的行(单元格)
            /// </summary>
            /// <param name="repeater">Repeater对象</param>
            /// <param name="cellID">某个单元格ID</param>
            public static void GroupRow(Repeater repeater, string cellID)
            {
                int count = repeater.Items.Count;
                for (int i = count - 1; i > 0; i--)
                {
                    RepeaterItem rpTprevious = repeater.Items[i - 1];
                    RepeaterItem rpT = repeater.Items[i];
    
                    MergeCells(rpTprevious, rpT, cellID, cellID);
                }
            }
    
            /// <summary>
            /// 合并Repeater中某行相同信息的行(单元格)
            /// </summary>
            /// <param name="repeater">Repeater对象</param>
            /// <param name="cellIDs">某些单元格ID</param>
            public static void GroupRow(Repeater repeater, params string[] cellIDs)
            {
                int count = repeater.Items.Count;
                for (int i = count - 1; i > 0; i--)
                {
                    RepeaterItem rpTprevious = repeater.Items[i - 1];
                    RepeaterItem rpT = repeater.Items[i];
    
                    for (int j = 0; j < cellIDs.Length; j++)
                    {
                        MergeCells(rpTprevious, rpT, cellIDs[j], cellIDs[0]);
                    }              
                }
            }
    
            /// <summary>
            /// 设置Repeater行背景的间隔颜色
            /// </summary>
            /// <param name="repeater">Repeater对象</param>       
            /// <param name="firRowID">第一行ID</param>
            /// <param name="secRowID">第二行ID</param>
            public static void SetBackgroundColor(Repeater repeater,  string firRowID, string secRowID)
            {
                int count = repeater.Items.Count;
                int i = 0;
                while (i < count)
                {
                    HtmlTableRow htmRow = repeater.Items[i].FindControl(firRowID) as HtmlTableRow;
                    HtmlTableRow htmRow2 = repeater.Items[i].FindControl(secRowID) as HtmlTableRow;
    
                    if (htmRow != null)
                    {
                        htmRow.Style.Add("background-color", "#B5F7C0");                  
                    }
    
                    if (htmRow2 != null)
                    {
                        htmRow2.Style.Add("background-color", "#B5F7C0");
                    }
    
                    i++;
                    if (i < count)
                    {
                        htmRow = repeater.Items[i].FindControl(firRowID) as HtmlTableRow;
                        htmRow2 = repeater.Items[i].FindControl(secRowID) as HtmlTableRow;
    
                        if (htmRow != null)
                        {
                            htmRow.Style.Add("background-color", "");                      
                        }
                        if (htmRow2 != null)
                        {                        
                            htmRow2.Style.Add("background-color", "");
                        }
                        i++;
                    }
                }
            }
    
            /// <summary>
            /// 设置Repeater行背景的间隔颜色
            /// </summary>
            /// <param name="repeater">Repeater对象</param>       
            /// <param name="firRowID">第一行ID</param>
            /// <param name="secRowID">第二行ID</param>
            public static void SetBackgroundColorByRow(Repeater repeater, string firRowID, string secRowID,string thiRowID)
            {
                int count = repeater.Items.Count;
                int i = 0;
                while (i < count)
                {
                    HtmlTableRow htmRow = repeater.Items[i].FindControl(firRowID) as HtmlTableRow;
                    HtmlTableRow htmRow2 = repeater.Items[i].FindControl(secRowID) as HtmlTableRow;
                    HtmlTableRow htmRow3 = repeater.Items[i].FindControl(thiRowID) as HtmlTableRow;
    
                    if (htmRow != null && htmRow2 != null)
                    {
                        htmRow.Style.Add("background-color", "#B5F7C0");
                        htmRow2.Style.Add("background-color", "#B5F7C0");
                        htmRow3.Style.Add("background-color", "#B5F7C0");
                    }
    
                    i++;
                    if (i < count)
                    {
                        htmRow = repeater.Items[i].FindControl(firRowID) as HtmlTableRow;
                        htmRow2 = repeater.Items[i].FindControl(secRowID) as HtmlTableRow;
                        htmRow3 = repeater.Items[i].FindControl(secRowID) as HtmlTableRow;
    
                        if (htmRow != null && htmRow2 != null)
                        {
                            htmRow.Style.Add("background-color", "");
                            htmRow2.Style.Add("background-color", "");
                            htmRow3.Style.Add("background-color", "");
                        }
                        i++;
                    }
                }
            }
    
            /// <summary>
            /// 设置Repeater行背景的间隔颜色
            /// </summary>
            /// <param name="repeater">Repeater对象</param>
            /// <param name="mergeCellID">合并单元格ID</param>
            /// <param name="firRowID">第一行ID</param>
            /// <param name="secRowID">第二行ID</param>
            public static void SetBackgroundColor(Repeater repeater, string mergeCellID, string firRowID, string secRowID)
            {
                int count = repeater.Items.Count;
                int i = 0;
                while (i < count)
                {
                    HtmlTableCell tc = repeater.Items[i].FindControl(mergeCellID) as HtmlTableCell;
    
                    if (tc.Visible)
                    {
                        int start = tc.RowSpan / 2 + i;
    
                        for (int j = i; j < start; j++)
                        {
                            HtmlTableRow htmRow = repeater.Items[j].FindControl(firRowID) as HtmlTableRow;
                            HtmlTableRow htmRow2 = repeater.Items[j].FindControl(secRowID) as HtmlTableRow;
    
                            if (htmRow != null && htmRow2 != null)
                            {
                                htmRow.Style.Add("background-color", "#B5F7C0");
                                htmRow2.Style.Add("background-color", "#B5F7C0");
                            }                     
                        }
                        i = start;
                    }
                    else
                    {
                        i++;
                    }
    
                    if (i < count)
                    {
                        tc = repeater.Items[i].FindControl(mergeCellID) as HtmlTableCell;
                        if (tc.Visible)
                        {
                            int start = tc.RowSpan / 2 + i;
    
                            for (int j = i; j < start; j++)
                            {
                                HtmlTableRow htmRow = repeater.Items[j].FindControl(firRowID) as HtmlTableRow;
                                HtmlTableRow htmRow2 = repeater.Items[j].FindControl(secRowID) as HtmlTableRow;
    
                                if (htmRow != null && htmRow2 != null)
                                {
                                    htmRow.Style.Add("background-color", "");
                                    htmRow2.Style.Add("background-color", "");
                                }                          
                            }
                            i = start;
                        }
                        else
                        {
                            i++;
                        }
                    }
                }
            }
    
            private static void MergeCells(RepeaterItem rpTprevious, RepeaterItem rpT, string cellID, string keyCellID)
            {
                HtmlTableCell oCell_previous = rpTprevious.FindControl(cellID) as HtmlTableCell;
                HtmlTableCell oCell = rpT.FindControl(cellID) as HtmlTableCell;
                if (oCell_previous != null && oCell != null)
                {
                    oCell.RowSpan = (oCell.RowSpan == -1) ? 1 : oCell.RowSpan;
                    oCell_previous.RowSpan = (oCell_previous.RowSpan == -1) ? 1 : oCell_previous.RowSpan;
    
                    if (oCell.Controls.Count > 1 && oCell_previous.Controls.Count > 1)
                    {
                        bool needMerge = false;
                        if (oCell.Controls[1] is LinkButton)
                        {
                            LinkButton licPre = oCell.Controls[1] as LinkButton;
                            LinkButton lic = oCell_previous.Controls[1] as LinkButton;
                            if (licPre != null && lic != null && lic.Text.Trim() == licPre.Text.Trim())
                            {
                                needMerge = true;
                            }
                        }
                        else if (oCell.Controls[1] is Label)
                        {
                            Label licPre = oCell.Controls[1] as Label;
                            Label lic = oCell_previous.Controls[1] as Label;
                            if (licPre != null && lic != null && lic.Text.Trim() == licPre.Text.Trim())
                            {
                                needMerge = true;
                            }
                        }
    
                        if (needMerge)
                        {
                            if (HasMerge(rpTprevious, rpT, cellID, keyCellID))
                            {
                                return;
                            }
    
                            oCell.Visible = false;
                            oCell_previous.RowSpan += oCell.RowSpan;
                        }                  
                    }
                    else if (oCell.InnerText == oCell_previous.InnerText)
                    {                   
                        if (HasMerge(rpTprevious, rpT, cellID, keyCellID))
                        {
                            return;
                        }
    
                        oCell.Visible = false;
                        oCell_previous.RowSpan += oCell.RowSpan;
                    }
                }
            }
    
            private static bool HasMerge(RepeaterItem rpTprevious, RepeaterItem rpT, string cellID, string keyCellID)
            {
                if (cellID != keyCellID)
                {
                    HtmlTableCell keyCellPrev = rpTprevious.FindControl(keyCellID) as HtmlTableCell;
                    HtmlTableCell keyCell = rpT.FindControl(keyCellID) as HtmlTableCell;
    
                    if (keyCellPrev != null && keyCell != null)
                    {
                        if (keyCellPrev.Controls.Count > 1 && keyCell.Controls.Count > 1)
                        {                        
                            if (keyCell.Controls[1] is LinkButton)
                            {
                                LinkButton licPre = keyCell.Controls[1] as LinkButton;
                                LinkButton lic = keyCellPrev.Controls[1] as LinkButton;
                                if (licPre != null && lic != null && lic.Text.Trim() != licPre.Text.Trim())
                                {
                                    return true;
                                }
                            }
                            else if (keyCell.Controls[1] is Label)
                            {
                                Label licPre = keyCell.Controls[1] as Label;
                                Label lic = keyCellPrev.Controls[1] as Label;
                                if (licPre != null && lic != null && lic.Text.Trim() != licPre.Text.Trim())
                                {
                                    return true;
                                }
                            }                      
                        }
                        else if (keyCellPrev.InnerText.Trim() != keyCell.InnerText.Trim())
                        {
                            return true;
                        }                  
                    }
                }
                return false;
            }
        }
    }

    引用实例:

    this.rpt_request.DataSource = ds.Tables[0];
    this.rpt_request.DataBind();
    RepeaterSetting.SetBackgroundColor(rpt_request, "Td2", "row1", "row2"); 

    另一种方式:

    private void SetRepeaterProperties()
            {
                foreach (RepeaterItem item in rpt_request.Items)
                {
                    HtmlTableCell maxNum = item.FindControl("MaxNumCol") as HtmlTableCell;
                    HtmlTableCell enrolmentNum = item.FindControl("EnrolmentNumCol") as HtmlTableCell;
                    if (maxNum != null && enrolmentNum != null)
                    {
                        HtmlTableRow htmRow = item.FindControl("row1") as HtmlTableRow;
                        HtmlTableRow htmRow2 = item.FindControl("row2") as HtmlTableRow;
    
                        int maxNum_value =Convert.ToInt32(maxNum.InnerText.Trim());
                        int enrolmentNum_value =Convert.ToInt32(enrolmentNum.InnerText.Trim());
    
                        if (enrolmentNum_value >= maxNum_value)
                        {
                            if (htmRow != null && htmRow2 != null)
                            {
                                htmRow.Style.Add("background-color", "Red");
                                htmRow2.Style.Add("background-color", "Red");
                            }
                        }
    
                        if (enrolmentNum_value == 0)
                        {
                            if (htmRow != null && htmRow2 != null)
                            {
                                htmRow.Style.Add("background-color", "Yellow");
                                htmRow2.Style.Add("background-color", "Yellow");
                            }
                        }
    
                    }
                    
                }
            }
  • 相关阅读:
    解决winfrom下TextBox不支持透明背景色
    C# Winform 怎么让按钮在Panel里居中显示
    DevExpress Cpicturebox或者Dev控件 PictureEdit 按比例的缩放加载图片
    Alluxio : 开源分布式内存文件系统
    yarn cluster和yarn client模式区别——yarn-cluster适用于生产环境,结果存HDFS;而yarn-client适用于交互和调试,也就是希望快速地看到application的输出
    Linux 反弹shell(二)反弹shell的本质
    浅谈摄像头有关的安全问题
    pyspark AttributeError: 'NoneType' object has no attribute 'setCallSite'
    大规模异常滥用检测:基于局部敏感哈希算法——来自Uber Engineering的实践
    pyspark minHash LSH 查找相似度
  • 原文地址:https://www.cnblogs.com/captainR/p/Repeater.html
Copyright © 2011-2022 走看看