Repeater05-Repeater重复数据的合并

本文介绍如何在ASP.NET中实现Repeater控件的重复数据合并。通过前台代码设置ID并添加服务器标志,配合后台代码中的循环处理,可以实现整个Repeater的重复数据合并。关键在于处理好前台命名和后台循环中的列数问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前台代码:

<%@ 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开始命名,并加上服务器标志,在后台的话,则是循环时的列数需要注意。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值