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="CompareTestEntity"> <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> <%# ((System.Data.DataRowView)Container.DataItem)["RegionSalesManID"]%> </td> <td> <%# ((System.Data.DataRowView)Container.DataItem)["AreaCode"]%> </td> <td> <%# ((System.Data.DataRowView)Container.DataItem)["AreaName"]%> </td> <td> <%# ((System.Data.DataRowView)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 DataView GetData { get { return Cache["_data"] as DataView; } set { if (Cache["_data"] == null) Cache["_data"] = value; } } private void BindRepeater() { DataTable dt = new DataTable("test"); DataColumn dc1 = new DataColumn("RegionSalesManID", typeof(System.String)); DataColumn dc2 = new DataColumn("AreaCode", typeof(System.String)); DataColumn dc3 = new DataColumn("AreaName", typeof(System.String)); DataColumn dc4 = new DataColumn("AreaSalesManID", typeof(System.String)); dt.Columns.Add(dc1); dt.Columns.Add(dc2); dt.Columns.Add(dc3); dt.Columns.Add(dc4); DataRow dr1 = dt.NewRow(); dr1["RegionSalesManID"] = "aabbcc123"; dr1["AreaCode"] = "abc123"; dr1["AreaName"] = "FENGHUAN1"; dr1["AreaSalesManID"] = "ABC123"; dt.Rows.Add(dr1); DataRow dr2 = dt.NewRow(); dr2["RegionSalesManID"] = "aabbcc124"; dr2["AreaCode"] = "abc124"; dr2["AreaName"] = "FENGHUAN2"; dr2["AreaSalesManID"] = "ABC124"; dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow(); dr3["RegionSalesManID"] = "aabbcc125"; dr3["AreaCode"] = "abc125"; dr3["AreaName"] = "FENGHUAN3"; dr3["AreaSalesManID"] = "ABC125"; dt.Rows.Add(dr3); DataSet ds = new DataSet(); ds.Tables.Add(dt); GetData = ds.Tables[0].DefaultView; Repeater1.DataSource = ds; Repeater1.DataBind(); } protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.Item.ItemType == ListItemType.Header) { 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"; } } DataView dv = GetData; dv.Sort = e.CommandName.ToString().Trim() + " " + ViewState[e.CommandName.Trim()].ToString().Trim(); Repeater1.DataSource = dv; Repeater1.DataBind(); } } } }
?