default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RepeaterDemoTest._Default" %> <!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"> <%@ Import Namespace="TestViewStateDemo.fh" %> <head id="Head1" 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_ArrItemCommand"> <HeaderTemplate> <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="AreaSalesManID" runat="server" Text="地区销售主任代码" CommandName="AreaSalesManID"></asp:LinkButton> </td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# ((TestEntity)Container.DataItem).RegionSalesManID%> </td> <td> <%# ((TestEntity)Container.DataItem).AreaCode%> </td> <td> <%# ((TestEntity)Container.DataItem).AreaName%> </td> <td> <%# ((TestEntity)Container.DataItem).AreaSalesManID%> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html>
?
default.aspx.cs
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.Collections; using System.Data.SqlClient; using TestViewStateDemo.fh; using System.Reflection; namespace RepeaterDemoTest { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindRepeater(); } } private List<TestEntity> GetDataArr { get { return Cache["_data"] as List<TestEntity>; } set { if (Cache["_data"] == null) Cache["_data"] = value; } } private void BindRepeater() { List<TestEntity> ar = new List<TestEntity>(); TestEntity te2 = new TestEntity(); te2.RegionSalesManID = "aabbcc124"; te2.AreaCode = "abc124"; te2.AreaName = "FENGHUAN2"; te2.AreaSalesManID = "ABC124"; ar.Add(te2); TestEntity te1 = new TestEntity(); te1.RegionSalesManID = "aabbcc123"; te1.AreaCode = "abc123"; te1.AreaName = "FENGHUAN1"; te1.AreaSalesManID = "ABC123"; ar.Add(te1); TestEntity te3 = new TestEntity(); te3.RegionSalesManID = "aabbcc125"; te3.AreaCode = "abc125"; te3.AreaName = "FENGHUAN3"; te3.AreaSalesManID = "ABC125"; ar.Add(te3); GetDataArr = ar; Repeater1.DataSource = ar; Repeater1.DataBind(); } protected void Repeater1_ArrItemCommand(object source, RepeaterCommandEventArgs e) { var l = Repeater1.DataSource; if (e.Item.ItemType == ListItemType.Header) { List<TestEntity> arr = GetDataArr; arr.Sort( delegate(TestEntity te1, TestEntity te2) { return CompareTestEntity(te1, te1, e); }); Repeater1.DataSource = arr; Repeater1.DataBind(); } } private int CompareTestEntity( TestEntity te1, TestEntity te2, RepeaterCommandEventArgs e) { Type type = typeof(TestEntity); object value1; object value2; PropertyInfo p = null; if (ViewState[e.CommandName.Trim()] == null) { ViewState[e.CommandName.Trim()] = "ASC"; } else { if (ViewState[e.CommandName.Trim()].ToString().Trim() == "ASC") { ViewState[e.CommandName.Trim()] = "DESC"; } else { ViewState[e.CommandName.Trim()] = "ASC"; } } p = type.GetProperty(e.CommandName); value1 = p.GetValue(te1, null); value2 = p.GetValue(te2, null); int comparedResult = ((IComparable)value1).CompareTo((IComparable)value2); if (ViewState[e.CommandName.Trim()] == "DESC") { comparedResult *= -1; } return comparedResult; } } }
?