ListView控件学习系列3-ListView选择,排序,分页
ListView控件学习系列3-ListView选择,排序,分页
目录:
ListView控件学习系列3-ListView选择,排序,分页
作者:Lance
选择例子
前端代码:
前端代码
<asp:ListView ID="ListView1" runat="server"
onselectedindexchanging="ListView1_SelectedIndexChanging">
<LayoutTemplate>
<table id="Table1" runat="server" border="0" style="">
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("ID") %>
</td>
<td>
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("name") %>' />
</td>
<td>
<asp:Label ID="xLabel" runat="server" Text='<%# Eval("age") %>' />
</td>
<td>
<asp:Button ID="SelectButton" CommandName="Select" runat="server" Text="选择" />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr style="background-color:Blue">
<td>
<%#Eval("ID") %>
</td>
<td>
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("name") %>' />
</td>
<td>
<asp:Label ID="xLabel" runat="server" Text='<%# Eval("age") %>' />
</td>
<td>
<asp:Button ID="SelectButton" CommandName="Select" runat="server" Text="选择" />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:ListView ID="ListView1" runat="server"
onselectedindexchanging="ListView1_SelectedIndexChanging">
<LayoutTemplate>
<table id="Table1" runat="server" border="0" style="">
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("ID") %>
</td>
<td>
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("name") %>' />
</td>
<td>
<asp:Label ID="xLabel" runat="server" Text='<%# Eval("age") %>' />
</td>
<td>
<asp:Button ID="SelectButton" CommandName="Select" runat="server" Text="选择" />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr style="background-color:Blue">
<td>
<%#Eval("ID") %>
</td>
<td>
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("name") %>' />
</td>
<td>
<asp:Label ID="xLabel" runat="server" Text='<%# Eval("age") %>' />
</td>
<td>
<asp:Button ID="SelectButton" CommandName="Select" runat="server" Text="选择" />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
后台代码:
后台代码
protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
ListView1.SelectedIndex = e.NewSelectedIndex;
Bind();
}
protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
ListView1.SelectedIndex = e.NewSelectedIndex;
Bind();
}
排序前端代码:
前端代码
<asp:ListView ID="ListView1" runat="server" OnSorting="ListView1_Sorting">
<LayoutTemplate>
<table id="Table1" runat="server" border="0" style="">
<tr>
<td>
<asp:LinkButton ID="LinkButton1" CommandArgument="ID" runat="server" CommandName="Sort">ID</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton2" CommandArgument="Name" runat="server" CommandName="Sort">Name</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton3" CommandArgument="Age" runat="server" CommandName="Sort">Age</asp:LinkButton>
</td>
</tr>
<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>
<asp:ListView ID="ListView1" runat="server" OnSorting="ListView1_Sorting">
<LayoutTemplate>
<table id="Table1" runat="server" border="0" style="">
<tr>
<td>
<asp:LinkButton ID="LinkButton1" CommandArgument="ID" runat="server" CommandName="Sort">ID</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton2" CommandArgument="Name" runat="server" CommandName="Sort">Name</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton3" CommandArgument="Age" runat="server" CommandName="Sort">Age</asp:LinkButton>
</td>
</tr>
<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>
排序模拟数据提供代码:
排序模拟数据提供代码
namespace Data
{
/// <summary>
///DataAccess 的摘要说明
/// </summary>
public class DataAccess
{
public List<Employee> List;
public DataAccess()
{
List = new List<Employee>();
Employee e1 = new Employee { ID = 1, Name = "A", Age = 10 };
Employee e2 = new Employee { ID = 3, Name = "M", Age = 30 };
Employee e3 = new Employee { ID = 2, Name = "B", Age = 40 };
Employee e4 = new Employee { ID = 4, Name = "S", Age = 19 };
Employee e5 = new Employee { ID = 6, Name = "X", Age = 18 };
Employee e6 = new Employee { ID = 5, Name = "W", Age = 20 };
List.Add(e1);
List.Add(e2);
List.Add(e3);
List.Add(e4);
List.Add(e5);
List.Add(e6);
}
public void SortID( string sortDirection)
{
if (sortDirection == "ASC")
{
List.Sort(new EmployeeIDAscCompare());
}
else
{
List.Sort(new EmployeeIDDescCompare());
}
}
}
class EmployeeIDAscCompare:IComparer<Employee>
{
#region IComparer<Employee> 成员
public int Compare(Employee x, Employee y)
{
return x.ID.CompareTo(y.ID);
}
#endregion
}
class EmployeeIDDescCompare : IComparer<Employee>
{
#region IComparer<Employee> 成员
public int Compare(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; }
}
}
namespace Data
{
/// <summary>
///DataAccess 的摘要说明
/// </summary>
public class DataAccess
{
public List<Employee> List;
public DataAccess()
{
List = new List<Employee>();
Employee e1 = new Employee { ID = 1, Name = "A", Age = 10 };
Employee e2 = new Employee { ID = 3, Name = "M", Age = 30 };
Employee e3 = new Employee { ID = 2, Name = "B", Age = 40 };
Employee e4 = new Employee { ID = 4, Name = "S", Age = 19 };
Employee e5 = new Employee { ID = 6, Name = "X", Age = 18 };
Employee e6 = new Employee { ID = 5, Name = "W", Age = 20 };
List.Add(e1);
List.Add(e2);
List.Add(e3);
List.Add(e4);
List.Add(e5);
List.Add(e6);
}
public void SortID( string sortDirection)
{
if (sortDirection == "ASC")
{
List.Sort(new EmployeeIDAscCompare());
}
else
{
List.Sort(new EmployeeIDDescCompare());
}
}
}
class EmployeeIDAscCompare:IComparer<Employee>
{
#region IComparer<Employee> 成员
public int Compare(Employee x, Employee y)
{
return x.ID.CompareTo(y.ID);
}
#endregion
}
class EmployeeIDDescCompare : IComparer<Employee>
{
#region IComparer<Employee> 成员
public int Compare(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();
}
}
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" NextPageText="Next"
PreviousPageText="Previous" />
</Fields>
</asp:DataPager>
</div>
<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" NextPageText="Next"
PreviousPageText="Previous" />
</Fields>
</asp:DataPager>
</div>
后台代码:
后台代码
protected void ListView1_PagePropertiesChanging1(object sender, PagePropertiesChangingEventArgs e)
{
Pager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
Bind();
}
protected void ListView1_PagePropertiesChanging1(object sender, PagePropertiesChangingEventArgs e)
{
Pager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
Bind();
}
作者:Lance
出处:http://www.cnblogs.com/nuaalfm/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。