网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 设计资源 > .Net编程 > 正文

ListView实现选择,排序,分页

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务

返回此系列教程目录

选择例子   【学习源码下载】

前端代码:

  1. <asp:ListView ID="ListView1" runat="server"  
  2.             onselectedindexchanging="ListView1_SelectedIndexChanging"> 
  3.             <LayoutTemplate> 
  4.                 <table id="Table1" runat="server" border="0" style=""> 
  5.                     <tr runat="server" id="itemPlaceholder" /> 
  6.                 </table> 
  7.             </LayoutTemplate> 
  8.             <ItemTemplate> 
  9.                 <tr> 
  10.                     <td> 
  11.                         <%#Eval("ID") %> 
  12.                     </td> 
  13.                     <td> 
  14.                         <asp:Label ID="idLabel" runat="server" Text='<%# Eval("name") %>' /> 
  15.                     </td> 
  16.                     <td> 
  17.                         <asp:Label ID="xLabel" runat="server" Text='<%# Eval("age") %>' /> 
  18.                     </td> 
  19.                     <td> 
  20.                     <asp:Button ID="SelectButton" CommandName="Select" runat="server" Text="选择" /> 
  21.                     </td> 
  22.                      
  23.                 </tr> 
  24.             </ItemTemplate> 
  25.             <SelectedItemTemplate> 
  26.             <tr style="background-color:Blue"> 
  27.                     <td> 
  28.                         <%#Eval("ID") %> 
  29.                     </td> 
  30.                     <td> 
  31.                         <asp:Label ID="idLabel" runat="server" Text='<%# Eval("name") %>' /> 
  32.                     </td> 
  33.                     <td> 
  34.                         <asp:Label ID="xLabel" runat="server" Text='<%# Eval("age") %>' /> 
  35.                     </td> 
  36.                     <td> 
  37.                     <asp:Button ID="SelectButton" CommandName="Select" runat="server" Text="选择" /> 
  38.                     </td> 
  39.                      
  40.                 </tr> 
  41.             </SelectedItemTemplate> 
  42.        </asp:ListView> 

后台代码:

  1. protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e) 
  2.     { 
  3.         ListView1.SelectedIndex = e.NewSelectedIndex; 
  4.         Bind(); 
  5.     } 

排序

前端代码:

  1. <asp:ListView ID="ListView1" runat="server" OnSorting="ListView1_Sorting"> 
  2.             <LayoutTemplate> 
  3.                 <table id="Table1" runat="server" border="0" style=""> 
  4.                     <tr> 
  5.                         <td> 
  6.                             <asp:LinkButton ID="LinkButton1" CommandArgument="ID" runat="server" CommandName="Sort">ID</asp:LinkButton> 
  7.                         </td> 
  8.                         <td> 
  9.                             <asp:LinkButton ID="LinkButton2" CommandArgument="Name" runat="server" CommandName="Sort">Name</asp:LinkButton> 
  10.                         </td> 
  11.                         <td> 
  12.                             <asp:LinkButton ID="LinkButton3" CommandArgument="Age" runat="server" CommandName="Sort">Age</asp:LinkButton> 
  13.                         </td> 
  14.                     </tr> 
  15.                     <tr runat="server" id="itemPlaceholder" /> 
  16.                 </table> 
  17.             </LayoutTemplate> 
  18.             <ItemTemplate> 
  19.                 <tr> 
  20.                     <td> 
  21.                         <%#Eval("ID") %> 
  22.                     </td> 
  23.                     <td> 
  24.                         <%# Eval("name") %> 
  25.                     </td> 
  26.                     <td> 
  27.                         <%# Eval("age") %> 
  28.                     </td> 
  29.                 </tr> 
  30.             </ItemTemplate> 
  31.         </asp:ListView> 

排序模拟数据提供代码:

  1. namespace Data 
  2.     /// <summary> 
  3.     ///DataAccess 的摘要说明 
  4.     /// </summary> 
  5.     public class DataAccess 
  6.     { 
  7.         public List<Employee> List; 
  8.         public DataAccess() 
  9.         { 
  10.             List = new List<Employee>(); 
  11.             Employee e1 = new Employee { ID = 1, Name = "A", Age = 10 }; 
  12.             Employee e2 = new Employee { ID = 3, Name = "M", Age = 30 }; 
  13.             Employee e3 = new Employee { ID = 2, Name = "B", Age = 40 }; 
  14.             Employee e4 = new Employee { ID = 4, Name = "S", Age = 19 }; 
  15.             Employee e5 = new Employee { ID = 6, Name = "X", Age = 18 }; 
  16.             Employee e6 = new Employee { ID = 5, Name = "W", Age = 20 }; 
  17.             List.Add(e1); 
  18.             List.Add(e2); 
  19.             List.Add(e3); 
  20.             List.Add(e4); 
  21.             List.Add(e5); 
  22.             List.Add(e6); 
  23.         } 
  24.         public void SortID( string sortDirection) 
  25.         { 
  26.             if (sortDirection == "ASC"
  27.             { 
  28.                 List.Sort(new EmployeeIDAscCompare()); 
  29.             } 
  30.             else 
  31.             { 
  32.                 List.Sort(new EmployeeIDDescCompare()); 
  33.             } 
  34.         } 
  35.     } 
  36.     class EmployeeIDAscCompare:IComparer<Employee> 
  37.     { 
  38.  
  39.         #region IComparer<Employee> 成员 
  40.  
  41.         public int Compare(Employee x, Employee y) 
  42.         { 
  43.             return x.ID.CompareTo(y.ID); 
  44.         } 
  45.  
  46.         #endregion 
  47.     } 
  48.     class EmployeeIDDescCompare : IComparer<Employee> 
  49.     { 
  50.  
  51.         #region IComparer<Employee> 成员 
  52.  
  53.         public int Compare(Employee x, Employee y) 
  54.         { 
  55.             return y.ID.CompareTo(x.ID); 
  56.         } 
  57.  
  58.         #endregion 
  59.     } 
  60.      
  61.     public class Employee 
  62.     { 
  63.         public int ID { getset; } 
  64.         public string Name { getset; } 
  65.         public int Age { getset; } 
  66.         public int Sex { getset; } 
  67.     } 

排序后台代码:

  1. public partial class Default3 : System.Web.UI.Page 
  2.     DataAccess da; 
  3.     protected void Page_Load(object sender, EventArgs e) 
  4.     { 
  5.         if (Session["da"] == null
  6.         { 
  7.             da = new DataAccess(); 
  8.             Session["da"] = da; 
  9.         } 
  10.         else 
  11.         { 
  12.             da = Session["da"as DataAccess; 
  13.         } 
  14.         if (!IsPostBack) 
  15.         { 
  16.             Bind(); 
  17.         } 
  18.     } 
  19.     private void Bind() 
  20.     { 
  21.         ListView1.DataSource = da.List; 
  22.         ListView1.DataBind(); 
  23.     } 
  24.     protected void ListView1_Sorting(object sender, ListViewSortEventArgs e) 
  25.     { 
  26.         if (String.IsNullOrEmpty(e.SortExpression)) { return; } 
  27.         string direction = ""
  28.         if (ViewState["SortDirection"] != null
  29.             direction = ViewState["SortDirection"].ToString(); 
  30.  
  31.         if (direction == "ASC"
  32.             direction = "DESC"
  33.         else 
  34.             direction = "ASC"
  35.  
  36.         ViewState["SortDirection"] = direction; 
  37.  
  38.         da.SortID(direction); 
  39.         Bind(); 
  40.     } 

分页例子

前端代码:

  1. <asp:ListView ID="ListView1" runat="server" OnPagePropertiesChanging="ListView1_PagePropertiesChanging1"> 
  2.             <LayoutTemplate> 
  3.                 <table id="Table1" runat="server" border="0" style=""> 
  4.                     <tr runat="server" id="itemPlaceholder" /> 
  5.                 </table> 
  6.             </LayoutTemplate> 
  7.             <ItemTemplate> 
  8.                 <tr> 
  9.                     <td> 
  10.                         <%#Eval("ID") %> 
  11.                     </td> 
  12.                     <td> 
  13.                         <%# Eval("name") %> 
  14.                     </td> 
  15.                     <td> 
  16.                         <%# Eval("age") %> 
  17.                     </td> 
  18.                 </tr> 
  19.             </ItemTemplate> 
  20.         </asp:ListView> 
  21.     </div> 
  22.     <div style="padding: 10px; text-align: right;"> 
  23.         <asp:DataPager ID="Pager" runat="server" PagedControlID="ListView1" PageSize="2"> 
  24.             <Fields> 
  25.                 <asp:NumericPagerField ButtonCount="10" NextPageText="" PreviousPageText="" /> 
  26.                 <asp:NextPreviousPagerField FirstPageText="First" LastPageText="Last" NextPageText="Next" 
  27.                     PreviousPageText="Previous" /> 
  28.             </Fields> 
  29.         </asp:DataPager> 
  30.     </div> 

后台代码:

  1. protected void ListView1_PagePropertiesChanging1(object sender, PagePropertiesChangingEventArgs e) 
  2.     { 
  3.         Pager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); 
  4.         Bind(); 
  5.     } 
  • 下一篇资讯: ListView经典使用技巧
  • 设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
    版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师