以下代码是用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 )%></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();  
            }  
        }   
    }  

相关文章: