现在的位置: 首页 > 综合 > 正文

Repeater 双向排序

2013年12月04日 ⁄ 综合 ⁄ 共 9757字 ⁄ 字号 评论关闭

    protected void lbGrossProfitRate_Click(object sender, EventArgs e)
    {
        this.SetSortTip(sender);
    }
    private void SetSortTip(object sender)
    {
        //过于复杂,应采用head的方式
        LinkButton sendLinkButton = (LinkButton)sender;
        SortDirection sd = SortDirection.Ascending;
        if (sendLinkButton.Text.IndexOf("▼") > 0)
        {
            sd = SortDirection.Descending;
        }
        sd = sd == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;

        this.lbReserveDate.Text = this.lbReserveDate.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
        this.lbReserveQuantity.Text = this.lbReserveQuantity.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
        this.lbReserveTicketQuantity.Text = this.lbReserveTicketQuantity.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
        this.lbInParkQuantity.Text = this.lbInParkQuantity.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
        this.lbInParkTicketQuantity.Text = this.lbInParkTicketQuantity.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
        this.lbInParkRate.Text = this.lbInParkRate.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
        this.lbSaleAmount.Text = this.lbSaleAmount.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
        this.lbCommissionAmount.Text = this.lbCommissionAmount.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);
        this.lbGrossProfitRate.Text = this.lbGrossProfitRate.Text.Replace("▼", string.Empty).Replace("▲", string.Empty);

        sendLinkButton.Text += sd == SortDirection.Ascending ? " ▲" : " ▼";
        this.VsSortExpression = string.Format("{0} {1}", sendLinkButton.CommandName, sd == SortDirection.Ascending ? "ASC" : "DESC");
        this.BindData();
    }

<th><asp:LinkButton ID="lbReserveDate" runat="server" OnClick="lbReserveDate_Click" CommandName="ReserveDate">预订日期</asp:LinkButton></th>
                    <th><asp:LinkButton ID="lbReserveQuantity" runat="server" OnClick="lbReserveQuantity_Click" CommandName="ReserveQuantity">预订单数</asp:LinkButton></th>
                    <th><asp:LinkButton ID="lbReserveTicketQuantity" runat="server" OnClick="lbReserveTicketQuantity_Click" CommandName="ReserveTicketQuantity">预订票数</asp:LinkButton></th>
                    <th><asp:LinkButton ID="lbInParkQuantity" runat="server" OnClick="lbInParkQuantity_Click" CommandName="InParkQuantity">入园单数</asp:LinkButton></th>
                    <th><asp:LinkButton ID="lbInParkTicketQuantity" runat="server" OnClick="lbInParkTicketQuantity_Click" CommandName="InParkTicketQuantity">入园票数</asp:LinkButton></th>
                    <th><asp:LinkButton ID="lbInParkRate" runat="server" OnClick="lbInParkRate_Click"  CommandName="InParkRate">入园率</asp:LinkButton></th>
                    <th><asp:LinkButton ID="lbSaleAmount" runat="server" OnClick="lbSaleAmount_Click"  CommandName="SaleAmount">销售金额</asp:LinkButton></th>
                    <th><asp:LinkButton ID="lbCommissionAmount" runat="server" OnClick="lbCommissionAmount_Click"  CommandName="CommissionAmount">佣金金额</asp:LinkButton></th>
                    <th><asp:LinkButton ID="lbGrossProfitRate" runat="server" OnClick="lbGrossProfitRate_Click"  CommandName="GrossProfitRate">毛利率</asp:LinkButton></th>

 

 DataColumn inParkRate = new DataColumn("InParkRate", typeof(decimal));
        DataColumn grossProfitRate = new DataColumn("GrossProfitRate", typeof(decimal));
        dt.Columns.Add(inParkRate);
        dt.Columns.Add(grossProfitRate);
        foreach (DataRow dr in dt.Rows)
        {
            dr["InParkRate"] = this.GetInParkRate(dr["InParkTicketQuantity"], dr["ReserveTicketQuantity"]);
            dr["GrossProfitRate"] = this.GetGrossProfitRate(dr["CommissionAmount"], dr["SaleAmount"]);
        }
        DataView dv = dt.DefaultView;
        dv.Sort = this.VsSortExpression;

 

//方便分面

    public string VsSortExpression
    {
        get
        {
            if (ViewState["sortExpression"] == null)
                ViewState["sortExpression"] = string.Empty;
            return (string)ViewState["sortExpression"];
        }
        set { ViewState["sortExpression"] = value; }
    }

 

建议使用DataView

    public SortDirection GridViewSortDirection
    {

        get
        {

            if (ViewState["sortDirection"] == null)

                ViewState["sortDirection"] = SortDirection.Ascending;

            return (SortDirection)ViewState["sortDirection"];

        }

        set { ViewState["sortDirection"] = value; }

    }

 

 protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
    {

        string sortExpression = e.SortExpression;

        if (GridViewSortDirection == SortDirection.Ascending)
        {

            GridViewSortDirection = SortDirection.Descending;

            SortGridView(sortExpression, " DESC");

        }

        else
        {

            GridViewSortDirection = SortDirection.Ascending;

            SortGridView(sortExpression, " ASC");

        }

    }

    private void SortGridView(string sortExpression, string direction)
    {

        DataTable dt = GetBind();

        //DataView dv = new DataView(dt);

        dt.DefaultView.Sort = sortExpression + direction;

        GridView1.DataSource = Pagination(dt, Pager1, false);

        GridView1.DataBind();

    }

 

===================================

 

以下转自http://www.cnblogs.com/vaiyanzi/archive/2007/03/23/685871.html

 

做项目的时候,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 窗体设计器生成的代码
【上篇】
【下篇】

抱歉!评论已关闭.