用Repeater控件实现复合表头和排序功能

以下代码是用Repeater控件实现复合表头、排序功能,最后附本程序运行效果图:

A:WebForm.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="WebApplication1.WebForm" %>  
 
<!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>无标题页</title>  
</head>  
<body>  
    <form id="Form1" method="post" runat="server">  
          
        <table width="100%" border=1 bordercolor=#330033 bordercolordark=#999933 bordercolorlight=black>  
            <tr  width="100%">  
            <td colspan=20 align=center><font color=blue size=12>Repeater实现双向排序功能</font></td>  
            </tr>  
              
           <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound">  
               <HeaderTemplate>  
                   <tr>  
<TD  align="center"  height="99" rowSpan="2"><asp:LinkButton ID="RegionCode" Runat="server" text="区域代码" CommandName="RegionCode"></asp:LinkButton></TD>  
<TD rowSpan=2 align="center" class="cssStrong"><asp:LinkButton ID="RegionName" Runat="server" text="区域名称" CommandName="RegionName"></asp:LinkButton></TD>  
<TD rowSpan=2 align="center" class="cssStrong"><asp:LinkButton ID="RegionDesc" Runat="server" text="区域描述" CommandName="RegionDesc"></asp:LinkButton></TD>  
<TD colSpan=8 align="center" class="cssStrong">省的基本信息</TD>  
<TD colSpan=7 align="center" class="cssStrong">城市的基本信息</TD>  
</TR>  
<TR>  
<TD align="center" class="cssStrong"><asp:LinkButton ID="RegionSalesManID" Runat="server" text="区域销售主任代码" CommandName="RegionSalesManID"></asp:LinkButton></TD>  
<TD class="cssStrong" ><asp:LinkButton ID="AreaCode" Runat="server" text="地区代码" CommandName="AreaCode"></asp:LinkButton> </TD>  
<TD class="cssStrong"><asp:LinkButton ID="AreaName" Runat="server" text="地区名称" CommandName="AreaName"></asp:LinkButton> </TD>  
<TD class="cssStrong"><asp:LinkButton ID="AreaDesc" Runat="server" text="地区描述" CommandName="AreaDesc"></asp:LinkButton></TD>  
<TD class="cssStrong"><asp:LinkButton ID="AreaSalesManID" Runat="server" text="地区销售主任代码" CommandName="AreaSalesManID"></asp:LinkButton></TD>  
<TD class="cssStrong"><asp:LinkButton ID="ProvinceCode" Runat="server" text="省代码" CommandName="ProvinceCode"></asp:LinkButton></TD>  
<TD class="cssStrong"><asp:LinkButton ID="ProvinceName" Runat="server" text="省名称" CommandName="ProvinceName"></asp:LinkButton></TD>  
<TD class="cssStrong"><asp:LinkButton ID="ProvinceDesc" Runat="server" text="省描述" CommandName="ProvinceDesc"></asp:LinkButton></TD>  
<TD align="center" class="cssStrong"><asp:LinkButton ID="CityCode" Runat="server" text="城市代码" CommandName="CityCode"></asp:LinkButton></TD>  
<TD class="cssStrong"><asp:LinkButton ID="CityName" Runat="server" text="城市名称" CommandName="CityName"></asp:LinkButton></TD>  
<TD class="cssStrong"><asp:LinkButton ID="CityDesc" Runat="server" text="城市描述" CommandName="CityDesc"></asp:LinkButton></TD>  
<TD class="cssStrong"><asp:LinkButton ID="CTPCode" Runat="server" text="城市类型代码" CommandName="CTPCode"></asp:LinkButton></TD>  
<TD class="cssStrong"><asp:LinkButton ID="CTPName" Runat="server" text="城市类型名称" CommandName="CTPName"></asp:LinkButton></TD>  
<TD class="cssStrong"><asp:LinkButton ID="CTPDesc" Runat="server" text="城市类型描述" CommandName="CTPDesc"></asp:LinkButton></TD>  
</TR>  
            </HeaderTemplate>  
              
            <ItemTemplate>  
                        <tr>  
                            <td><%# DataBinder.Eval(Container.DataItem, "RegionCode")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "RegionName")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "RegionDesc")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "RegionSalesManID")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "AreaCode")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "AreaName")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "AreaDesc")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "AreaSalesManID")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "ProvinceCode")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "ProvinceName")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "ProvinceDesc")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "CityCode")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "CityName")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "CityDesc")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "CTPCode")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "CTPName")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "CTPDesc")%></td>                                                                                           
                        </tr>  
            </ItemTemplate>  
              
                <AlternatingItemTemplate>  
                        <tr bgcolor="lightyellow">  
                            <td><%# DataBinder.Eval(Container.DataItem, "RegionCode")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "RegionName")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "RegionDesc")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "RegionSalesManID")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "AreaCode")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "AreaName")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "AreaDesc")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "AreaSalesManID")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "ProvinceCode")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "ProvinceName")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "ProvinceDesc")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "CityCode")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "CityName")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "CityDesc")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "CTPCode")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "CTPName")%></td>  
                            <td><%# DataBinder.Eval(Container.DataItem, "CTPDesc")%></td>                                                                                           
                        </tr>  
               </AlternatingItemTemplate>  
 
                <FooterTemplate> </table>   </FooterTemplate>  
 
              
           </asp:Repeater>  
        </form>  
</body>  
</html> 

B: WebForm.aspx.cs
using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
using System.Data.SqlClient;  
 
namespace WebApplication1  
{  
    public partial class WebForm : System.Web.UI.Page  
    {  
        private readonly string CONNECTIONSTRING =System.Configuration.ConfigurationManager.AppSettings["ConnectString"];  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
                BindRepeater();  
            }  
        }  
 
        private DataView GetData  
        {  
            get 
            {  
                return Cache["_data"] as DataView;  
            }  
            set 
            {  
                if (Cache["_data"] == null)  
                    Cache["_data"] = value;  
            }  
        }  
 
        private SqlConnection Conn()  
        {  
            return new SqlConnection(CONNECTIONSTRING);  
        }  
 
        private void BindRepeater()  
        {  
            DataSet ds = new DataSet();  
            SqlDataAdapter da = new SqlDataAdapter("    SELECT RegionCode,RegionName,RegionDesc,RegionSalesManID=ta.SalesManID,"+  
                                                    "   AreaCode,AreaName,AreaDesc,AreaSalesManID=tb.SalesManID,"+  
                                                    "   ProvinceCode,ProvinceName,ProvinceDesc,"+  
                                                    "   CityCode,CityName,CityDesc,"+  
                                                    "   CTPCode=(SELECT CTPCode FROM CityType WHERE CTPID=TD.CTPID),"+  
                                                    "   CTPName=(SELECT CTPName FROM CityType WHERE CTPID=TD.CTPID),"+  
                                                    "   CTPDesc=(SELECT CTPDesc FROM CityType WHERE CTPID=TD.CTPID)"+  
                                                    "   FROM Region Ta,Area Tb,Province Tc,City Td"+  
                                                    "   WHERE Ta.RegionID=Tb.RgnID And Tb.AreaID=Tc.AreaID" +  
                                                    "   And Tc.ProvinceID=Td.ProvinceID", Conn());  
            da.Fill(ds);  
            GetData = ds.Tables[0].DefaultView;  
            Repeater1.DataSource = ds.Tables[0];  
            Repeater1.DataBind();  
        }  
 
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)  
        {  
            if (e.Item.ItemType == ListItemType.Header)  
            {  
                if (ViewState["id"] != null)  
                {  
                    LinkButton lkbtnSort = (LinkButton)e.Item.FindControl(ViewState["id"].ToString().Trim());  
                    lkbtnSort.Text = ViewState["text"].ToString();  
                }  
            }  
        }  
 
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)  
        {  
            if (e.Item.ItemType == ListItemType.Header)  
            {  
                LinkButton lkbtnSort = (LinkButton)e.Item.FindControl(e.CommandName.Trim());  
                if (ViewState[e.CommandName.Trim()] == null)  
                {  
                    ViewState[e.CommandName.Trim()] = "ASC";  
                    lkbtnSort.Text = lkbtnSort.Text + "▲";  
                }  
                else 
                {  
                    if (ViewState[e.CommandName.Trim()].ToString().Trim() == "ASC")  
                    {  
                        ViewState[e.CommandName.Trim()] = "DESC";  
                        if (lkbtnSort.Text.IndexOf("▲") != -1)  
                            lkbtnSort.Text = lkbtnSort.Text.Replace("▲", "▼");  
                        else 
                            lkbtnSort.Text = lkbtnSort.Text + "▼";  
                    }  
                    else 
                    {  
                        ViewState[e.CommandName.Trim()] = "ASC";  
                        if (lkbtnSort.Text.IndexOf("▼") != -1)  
                            lkbtnSort.Text = lkbtnSort.Text.Trim().Replace("▼", "▲");  
                        else 
                            lkbtnSort.Text = lkbtnSort.Text + "▲";  
                    }  
                }  
                ViewState["text"] = lkbtnSort.Text;  
                ViewState["id"] = e.CommandName.Trim();  
                DataView dv = GetData;  
                dv.Sort = e.CommandName.ToString().Trim() + " " + ViewState[e.CommandName.Trim()].ToString().Trim();  
                Repeater1.DataSource = dv;  
                Repeater1.DataBind();  
            }  
        }   
    }  

posted @ 2010-03-08 15:01  '.Elvis.'  阅读(1028)  评论(0编辑  收藏  举报