Repeater04-Repeater某列重复数据的合并
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterToRow.aspx.cs" Inherits="RepeaterToRow" %>
<%@ 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>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr id="trIdrep" οnmοuseοver="this.bgColor='#C4DFF7'" οnmοuseοut="this.bgColor='#ffffff'">
<td nowrap><%#Container.ItemIndex+1%> </td>
<td nowrap><%#Eval("userName")%></td>
<td nowrap id="userSex" runat="server"><%#Eval("userSex")%></td>
<td nowrap><%#Eval("userPhone")%></td>
<td nowrap><%#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 RepeaterToRow : 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();
//性别rowspan
for (int i = Repeater1.Items.Count - 1; i > 0; i--)
{
HtmlTableCell oCell_previous = Repeater1.Items[i - 1].FindControl("userSex") as HtmlTableCell;
HtmlTableCell oCell = Repeater1.Items[i].FindControl("userSex") as HtmlTableCell;
oCell.RowSpan = (oCell.RowSpan == -1) ? 1 : oCell.RowSpan;
oCell_previous.RowSpan = (oCell_previous.RowSpan == -1) ? 1 : oCell_previous.RowSpan;
if (oCell.InnerText == oCell_previous.InnerText)
{
oCell.Visible = false;
oCell_previous.RowSpan += oCell.RowSpan;
}
}
}
}
页面展示
注意要点
对于本案例中需要合并的性别列,在前台需要给td命名一个ID,并加上服务器标志记号,在后台查找这个ID进行数据的合并