are(Employee x, Employee y)
{
return y.ID.CompareTo(x.ID);
}
#endregion
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int Sex { get; set; }
}
}
排序后台代码:
后台代码
public partial class Default3 : System.Web.UI.Page
{
DataAccess da;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["da"] == null)
{
da = new DataAccess();
Session["da"] = da;
}
else
{
da = Session["da"] as DataAccess;
}
if (!IsPostBack)
{
Bind();
}
}
private void Bind()
{
ListView1.DataSource = da.List;
ListView1.DataBind();
}
protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
{
if (String.IsNullOrEmpty(e.SortExpression)) { return; }
string direction = "";
if (ViewState["SortDirection"] != null)
direction = ViewState["SortDirection"].ToString();
if (direction == "ASC")
direction = "DESC";
else
direction = "ASC";
ViewState["SortDirection"] = direction;
da.SortID(direction);
Bind();
}
}
分页例子:
前端代码
<asp:ListView ID="ListView1" runat="server" OnPagePropertiesChanging="ListView1_PagePropertiesChanging1">
<LayoutTemplate>
<table id="Table1" runat="server" border="0" style="">
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("ID") %>
</td>
<td>
<%# Eval("name") %>
</td>
<td>
<%# Eval("age") %>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</div>
<div style="padding: 10px; text-align: right;">
<asp:DataPager ID="Pager" runat="server" PagedControlID="ListView1" PageSize="2">
<Fields>
<asp:NumericPagerField ButtonCount="10" NextPageText="" PreviousPageText="" />
<asp:NextPreviousPagerField FirstPageText="First" LastPageText="Last" NextP
|