代码改变世界

Repeater 双向排序

2007-03-23 23:30  蓝之风  阅读(2873)  评论(6编辑  收藏  举报
做项目的时候,DataGrid ,DataList,Repeater 三个控件都是很优秀的数据显示控件,DataGrid的方便,简单易用,功能强大,但对性能会有所影响,在loading页面的时候大量的Html会占用一些时间,DataList 较之DataGrid功能显然减少一些,经常在一些特殊的页面制作或需求中来用,经常简单的页面显示收据表格,使用Repeater+Table即方便又快捷,但是却没有DataGrid那样方便,最近做项目,全部使用Repeater,但是客户突然要求要对数据表格进行排序,惨了~~~~~,第一印象就是DataGrid控件有排序功能,要换控件吗?靠,那么多表单,全部换DataGrid会出人命,于是就想办法,来实现Repeater的排序。下面就是我做的一个Repeater实现双向排序的例子:主要使用LinkButton,Repeater,ViewState,Cache结合来完成的。
效果如下图:

下载原代码:/Files/vaiyanzi/Attch/RepeaterOrder.rar
前台Html代码:
<%@ Page language="c#" Codebehind="RepeaterOrderForm.aspx.cs" AutoEventWireup="false" Inherits="RepeaterOrder.RepeaterOrderForm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>RepeaterOrderForm</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<table border=1 bordercolor=#330033 bordercolordark=#999933 bordercolorlight=black>
            
<tr>
            
<td colspan=5 align=center><font color=blue size=12>Repeater实现双向排序功能</font></td>
            
</tr>
                
<asp:Repeater id="rpOrder" runat="server">
                    
<HeaderTemplate>
                        
<tr align=center >
                            
<td>
                                
<asp:LinkButton ID="LastName" Runat="server" text="LastName" CommandName="LastName"></asp:LinkButton></td>
                            
<td>
                                
<asp:LinkButton ID="FirstName" Runat="server" text="FirstName" CommandName="FirstName"></asp:LinkButton></td>
                            
<td>
                                
<asp:LinkButton ID="Title" Runat="server" text="Title" CommandName="Title"></asp:LinkButton></td>
                            
<td>
                                
<asp:LinkButton ID="Address" Runat="server" text="Address" CommandName="Address"></asp:LinkButton></td>
                            
<td>
                                
<asp:LinkButton ID="City" Runat="server" text="City" CommandName="City"></asp:LinkButton></td>
                            
                        
</tr>
                    
</HeaderTemplate>
                    
<ItemTemplate>
                        
<tr>
                            
<td><%# DataBinder.Eval(Container.DataItem,"LastName")%></td>
                            
<td><%# DataBinder.Eval(Container.DataItem,"FirstName")%></td>
                            
<td><%# DataBinder.Eval(Container.DataItem,"Title")%></td>
                            
<td><%# DataBinder.Eval(Container.DataItem,"Address")%></td>
                            
<td><%# DataBinder.Eval(Container.DataItem,"City")%></td>                            
                        
</tr>
                    
</ItemTemplate>
                
</asp:Repeater>
            
</table>
        
</form>
    
</body>
</HTML>
后台CS文件:

  1using System;
  2using System.Collections;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Web;
  7using System.Web.SessionState;
  8using System.Web.UI;
  9using System.Web.UI.WebControls;
 10using System.Web.UI.HtmlControls;
 11using System.Data.SqlClient;
 12using System.Configuration;
 13
 14namespace RepeaterOrder
 15{
 16    /// <summary>
 17    /// RepeaterOrderForm 的摘要说明。
 18    /// </summary>

 19    public class RepeaterOrderForm : System.Web.UI.Page
 20    {
 21        protected System.Web.UI.WebControls.Repeater rpOrder;
 22        private readonly string CONNECTIONSTRING=ConfigurationSettings.AppSettings["ConnectionString"];
 23        private void Page_Load(object sender, System.EventArgs e)
 24        {
 25            if(!IsPostBack)
 26            {
 27                BindRepeater();
 28            }

 29        }

 30        private DataView GetData
 31        {
 32            get
 33            
 34                return Cache["_data"as DataView;
 35            }

 36            set
 37            {
 38                if( Cache["_data"]==null)
 39                    Cache["_data"]=value;
 40            }

 41        }

 42        Web 窗体设计器生成的代码
 64
 65        private SqlConnection Conn()
 66        {
 67            return new SqlConnection(CONNECTIONSTRING);
 68        }

 69
 70        private void BindRepeater()
 71        {
 72            DataSet ds=new DataSet();
 73            SqlDataAdapter da=new SqlDataAdapter("Select * From Employees",Conn());
 74            da.Fill(ds);
 75            GetData=ds.Tables[0].DefaultView;
 76            rpOrder.DataSource=ds.Tables[0];
 77            rpOrder.DataBind();
 78        }

 79
 80        private void rpOrder_ItemCommand(object source, RepeaterCommandEventArgs e)
 81        {
 82            if(e.Item.ItemType==ListItemType.Header)
 83            {            
 84                LinkButton lkbtnSort=(LinkButton)e.Item.FindControl(e.CommandName.Trim());                
 85                if(ViewState[e.CommandName.Trim()]==null)
 86                {
 87                    ViewState[e.CommandName.Trim()]="ASC";                            
 88                    lkbtnSort.Text=lkbtnSort.Text+"";
 89                }

 90                else
 91                {
 92                    if(ViewState[e.CommandName.Trim()].ToString().Trim()=="ASC")
 93                    {
 94                        ViewState[e.CommandName.Trim()]="DESC";    
 95                        if(lkbtnSort.Text.IndexOf("")!=-1)
 96                            lkbtnSort.Text=lkbtnSort.Text.Replace("","");
 97                        else
 98                            lkbtnSort.Text=lkbtnSort.Text+"";
 99                    }

100                    else
101                    {
102                        ViewState[e.CommandName.Trim()]="ASC";    
103                        if(lkbtnSort.Text.IndexOf("")!=-1)
104                            lkbtnSort.Text=lkbtnSort.Text.Trim().Replace("","");
105                        else
106                            lkbtnSort.Text=lkbtnSort.Text+"";
107                    }

108                }

109                ViewState["text"]=lkbtnSort.Text;
110                ViewState["id"]=e.CommandName.Trim();
111                DataView dv=GetData;
112                dv.Sort=e.CommandName.ToString().Trim()+" "+ViewState[e.CommandName.Trim()].ToString().Trim();
113                rpOrder.DataSource=dv;
114                rpOrder.DataBind();
115            }
            
116        }

117
118        private void rpOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
119        {
120            if(e.Item.ItemType==ListItemType.Header)
121            {
122                if(ViewState["id"]!=null)
123                {                    
124                    LinkButton lkbtnSort=(LinkButton)e.Item.FindControl(ViewState["id"].ToString().Trim());                        
125                    lkbtnSort.Text=ViewState["text"].ToString();
126                }

127            }

128        }

129    }

130}

131