Repeater05-Repeater重复数据的合并

来源:互联网 发布:有什么供应商软件 编辑:程序博客网 时间:2024/05/14 07:11

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterToRow2.aspx.cs" Inherits="RepeaterToRow2" %><%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <style>        #tablePrint {            width: 100%;            margin-bottom: 5px;        }            #tablePrint, #tablePrint th, #tablePrint td {                border: 1px solid #ccc;                border-collapse: collapse;                padding: 2px;            }                #tablePrint tr:nth-child(odd) {                    background-color: rgb(235, 240, 255);                }    </style></head><body>    <form id="form1" runat="server">        <div>            <asp:Repeater ID="Repeater1" runat="server">                <HeaderTemplate>                    <table id="tablePrint" class="tbShow">                        <tr class="th">                            <td nowrap width="35px;" align="center">序号</td>                            <td nowrap>学校</td>                            <td nowrap>班级</td>                            <td nowrap>姓名</td>                            <td nowrap>性别</td>                            <td nowrap>手机</td>                            <td nowrap>邮箱</td>                        </tr>                </HeaderTemplate>                <ItemTemplate>                    <tr id="trIdrep" onmouseover="this.bgColor='#C4DFF7'" onmouseout="this.bgColor='#ffffff'">                        <td nowrap id="td0" runat="server"><%#Container.ItemIndex+1%> </td>                        <td nowrap id="td1" runat="server"><%#Eval("userSchool")%></td>                        <td nowrap id="td2" runat="server"><%#Eval("userClass")%></td>                        <td nowrap id="td3" runat="server"><%#Eval("userName")%></td>                        <td nowrap id="td4" runat="server"><%#Eval("userSex")%></td>                        <td nowrap id="td5" runat="server"><%#Eval("userPhone")%></td>                        <td nowrap id="td6" runat="server"><%#Eval("userEmail")%></td>                    </tr>                </ItemTemplate>                <FooterTemplate>                    </table>                </FooterTemplate>            </asp:Repeater>        </div>    </form></body></html>

后台代码:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Web.UI.HtmlControls;public partial class RepeaterToRow2 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!this.IsPostBack)        {            DataBindRepeater();        }    }    private void DataBindRepeater()    {        string strSql = "select * from UserInfo";        DataSet ds = SqlHelper.ExecuteDataset(CommandType.Text, strSql);        this.Repeater1.DataSource = ds.Tables[0];        this.Repeater1.DataBind();        for (int i = 0; i < 7; i++) // 遍历每一列        {            string tdTd = "td";            string tdIdName = tdTd + i.ToString();            MergeCell(tdIdName); // 把当前列的td的ID文本作为方法的参数        }    }    /// <summary>    ///     /// </summary>    /// <param name="tdIdName">当前列当前行的 td 的ID文本</param>    private void MergeCell(string tdIdName)    {        for (int i = Repeater1.Items.Count - 1; i > 0; i--) // Repeater1.Items.Count - 1 数据总行数(数据从0开始)  遍历当前列的每一行        {            MergeCellSet(tdIdName, i);        }    }    /// <summary>    ///     /// </summary>    /// <param name="tdIdName1">当前列当前行的 td 的ID文本</param>    /// <param name="i">当前行</param>    private void MergeCellSet(string tdIdName1, int i)    {        HtmlTableCell cellPrev = Repeater1.Items[i - 1].FindControl(tdIdName1) as HtmlTableCell; // 获取下一行当前列的 td 所在的单元格        HtmlTableCell cell = Repeater1.Items[i].FindControl(tdIdName1) as HtmlTableCell; // 获取当前行当前列的 td 所在的单元格        cell.RowSpan = (cell.RowSpan == -1) ? 1 : cell.RowSpan; // 获取当前行当前列单元格跨越的行数         cellPrev.RowSpan = (cellPrev.RowSpan == -1) ? 1 : cellPrev.RowSpan; // 获取下一行当前列单元格跨越的行数         if (cell.InnerText == cellPrev.InnerText)        {            // 让下一行的当前单元格的跨越行数 + 当前行的跨越行数            cellPrev.RowSpan += cell.RowSpan;            cell.Visible = false;  // 隐藏当前行            //关键代码,再判断执行第2列的合并单元格方法        }    }}

页面展示:


注意要点:

之前的案例是某列的重复数据的合并,而此案例则是将整个Repeater都进行重复数据合并,就是对之前的方法加上循环。

前台注意的就是命名ID时从0开始命名,并加上服务器标志,在后台的话,则是循环时的列数需要注意。



0 0
原创粉丝点击