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

ListView经典使用技巧

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

返回此系列教程目录         【学习源码下载

1、鼠标移到ListView某一行时改变该行的背景色方法

前端代码:

  1. <asp:ListView ID="ListView1" runat="server"  
  2.             onitemdatabound="ListView1_ItemDataBound"> 
  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 runat="server" id="Tr"> 
  10.                     <td> 
  11.                         <%#Eval("ID") %> 
  12.                     </td> 
  13.                     <td> 
  14.                         <%# Eval("name") %> 
  15.                     </td> 
  16.                     <td> 
  17.                         <%# Eval("age") %> 
  18.                     </td> 
  19.                 </tr> 
  20.             </ItemTemplate> 
  21.         </asp:ListView> 

后台代码:

  1. protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) 
  2.     { 
  3.         if (e.Item.ItemType==ListViewItemType.DataItem) 
  4.         { 
  5.             (e.Item.FindControl("Tr"as HtmlTableRow).Attributes. 
  6.                 Add("onmouseover""c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'"); 
  7.             (e.Item.FindControl("Tr"as HtmlTableRow).Attributes. 
  8.             Add("onmouseout""this.style.backgroundColor=c"); 
  9.         } 
  10.     } 

2、ListView和和下拉菜单DropDownList结合(其他控件其实也差不多):
前端代码:

  1. <asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound"> 
  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 runat="server" id="Tr"> 
  9.                     <td> 
  10.                         <asp:Label ID="ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label> 
  11.                     </td> 
  12.                     <td> 
  13.                         <%# Eval("name") %> 
  14.                     </td> 
  15.                     <td> 
  16.                         <%# Eval("age") %> 
  17.                     </td> 
  18.                     <td> 
  19.                         <asp:DropDownList ID="SexDropDownList" runat="server"> 
  20.                         </asp:DropDownList> 
  21.                     </td> 
  22.                 </tr> 
  23.             </ItemTemplate> 
  24.         </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 ,Sex=1}; 
  12.             Employee e2 = new Employee { ID = 3, Name = "M", Age = 30 ,Sex=0}; 
  13.             Employee e3 = new Employee { ID = 2, Name = "B", Age = 40,Sex=1 }; 
  14.             Employee e4 = new Employee { ID = 4, Name = "S", Age = 19,Sex=0 }; 
  15.             Employee e5 = new Employee { ID = 6, Name = "X", Age = 18 ,Sex=1}; 
  16.             Employee e6 = new Employee { ID = 5, Name = "W", Age = 20 ,Sex=0}; 
  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 Employee Find( int id) 
  25.         { 
  26.             foreach (var item in List) 
  27.             { 
  28.                 if (item.ID == id) 
  29.                     return item; 
  30.             } 
  31.             return null
  32.         } 
  33.     } 
  34.     public class Employee 
  35.     { 
  36.         public int ID { getset; } 
  37.         public string Name { getset; } 
  38.         public int Age { getset; } 
  39.         public int Sex { getset; } 
  40.     } 

后台代码:

  1. protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) 
  2.     { 
  3.         DropDownList SexDropDownList = e.Item.FindControl("SexDropDownList"as DropDownList; 
  4.         int sex = da.Find(Convert.ToInt32((e.Item.FindControl("ID"as Label).Text)).Sex; 
  5.         ListItem listItem; 
  6.         if (sex == 0) 
  7.         { 
  8.             listItem = new ListItem("男", sex.ToString()); 
  9.         } 
  10.         else 
  11.         { 
  12.             listItem = new ListItem("女", sex.ToString()); 
  13.         } 
  14.         SexDropDownList.Items.Add(listItem); 
  15.         SexDropDownList.Enabled = false
  16.     } 

3、ListView实现删除时弹出确认对话框:
前端代码

  1. <asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound"> 
  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 runat="server" id="Tr"> 
  9.                     <td> 
  10.                         <asp:Label ID="ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label> 
  11.                     </td> 
  12.                     <td> 
  13.                         <%# Eval("name") %> 
  14.                     </td> 
  15.                     <td> 
  16.                         <%# Eval("age") %> 
  17.                     </td> 
  18.                     <td> 
  19.                         <asp:Button ID="Delete" runat="server" Text="Delete" /> 
  20.                     </td> 
  21.                 </tr> 
  22.             </ItemTemplate> 
  23.         </asp:ListView> 

后台代码

  1. protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) 
  2.     { 
  3.         if (e.Item.ItemType == ListViewItemType.DataItem) 
  4.         { 
  5.             (e.Item.FindControl("Delete"as Button).Attributes.Add 
  6.                 ("onclick""javascript:return confirm('你确认要删除:\""  
  7.                 + (e.Item.FindControl("id"as Label).Text + "\"吗?')"); 
  8.  
  9.         } 
  10.     } 

4、ListView实现自动编号

  1. <asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound"> 
  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 runat="server" id="Tr"> 
  9.                     <td> 
  10.                         <asp:Label ID="Num" runat="server"></asp:Label> 
  11.                     </td> 
  12.                     <td> 
  13.                         <asp:Label ID="ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label> 
  14.                     </td> 
  15.                     <td> 
  16.                         <%# Eval("name") %> 
  17.                     </td> 
  18.                     <td> 
  19.                         <%# Eval("age") %> 
  20.                     </td> 
  21.                     <td> 
  22.                         <asp:Button ID="Delete" runat="server" Text="Delete" /> 
  23.                     </td> 
  24.                 </tr> 
  25.             </ItemTemplate> 
  26.         </asp:ListView> 

后台代码

  1. int Num = 1; 
  2.     protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) 
  3.     { 
  4.         if (e.Item.ItemType == ListViewItemType.DataItem) 
  5.         { 
  6.             (e.Item.FindControl("Num"as Label).Text = Num.ToString(); 
  7.             Num++; 
  8.         } 
  9.     } 

5、将ListView中的内容以Excel导出

前端代码:

  1. <div> 
  2.      <asp:ListView ID="ListView1" runat="server"> 
  3.         <ItemTemplate> 
  4.             <tr> 
  5.                 <td id="Td1" runat="server" style=""> 
  6.                     <asp:Label ID="idLabel" runat="server" Text='<%# Eval("name") %>' /> 
  7.                     <br /> 
  8.                 </td> 
  9.                 <td> 
  10.                     <asp:Label ID="xLabel" runat="server" Text='<%# Eval("Age") %>' /> 
  11.                     <br /> 
  12.                 </td> 
  13.             </tr> 
  14.         </ItemTemplate> 
  15.         <LayoutTemplate> 
  16.             <table id="Table1" runat="server" border="0" style=""> 
  17.                 <tr runat="server" id="itemPlaceholder" /> 
  18.                  
  19.             </table> 
  20.         </LayoutTemplate> 
  21.     </asp:ListView> 
  22.     </div><asp:Button ID="Button1" runat="server" Text="Button"  
  23.             onclick="Button1_Click" /> 

后台代码

  1. public partial class ListView技巧_Code13 : 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.     private void Export(string FileType, string FileName) 
  25.     { 
  26.         Response.Charset = "GB2312"
  27.         Response.ContentEncoding = System.Text.Encoding.UTF7; 
  28.         Response.AppendHeader("Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); 
  29.         Response.ContentType = FileType; 
  30.         this.EnableViewState = false
  31.         StringWriter tw = new StringWriter(); 
  32.         HtmlTextWriter hw = new HtmlTextWriter(tw); 
  33.         ListView1.RenderControl(hw); 
  34.         Response.Write(tw.ToString()); 
  35.         Response.End(); 
  36.     } 
  37.  
  38.     protected void Button1_Click(object sender, EventArgs e) 
  39.     { 
  40.         Export("application/ms-excel""employee.xls"); 
  41.  
  42.     } 

6、高级分页

使用DataPager分页虽然简单但是对于大数据量时性能会成问题,因为这种分页仍然是一次性将数据载入到控件中的,暂时没找到使用DataPager好的解决方案,这里使用一个网上比较流行的开源的.net分页控件AspNetPager,下载地址为: http://www.webdiyer.com/

前端代码:

  1. <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %> 
  2.  
  3. <asp:ListView ID="ListView1" runat="server"> 
  4.         <LayoutTemplate> 
  5.             <table id="Table1" runat="server" border="0" style=""> 
  6.                 <tr runat="server" id="itemPlaceholder" /> 
  7.             </table> 
  8.         </LayoutTemplate> 
  9.         <ItemTemplate> 
  10.             <tr> 
  11.                 <td> 
  12.                     <%#Eval("ID") %> 
  13.                 </td> 
  14.                 <td> 
  15.                     <%# Eval("name") %> 
  16.                 </td> 
  17.                 <td> 
  18.                     <%# Eval("age") %> 
  19.                 </td> 
  20.             </tr> 
  21.         </ItemTemplate> 
  22.     </asp:ListView> 
  23.     <webdiyer:AspNetPager ID="AspNetPager1" runat="server" UrlPaging="false" ShowPageIndexBox="Always" 
  24.         PageIndexBoxType="DropDownList" ShowCustomInfoSection="Left" PageIndexBoxStyle="width:19px" 
  25.         CustomInfoHTML="共%PageCount%页,%RecordCount%条记录,当前第%CurrentPageIndex%页,每页显示%PageSize%条" 
  26.         HorizontalAlign="Center" PageSize="2" FirstPageText="首页" LastPageText="末页" NextPageText="" 
  27.         PrevPageText="前一页" TextAfterPageIndexBox="页" TextBeforePageIndexBox="转到第" EnableTheming="true" 
  28.         OnPageChanging="AspNetPager1_PageChanging"> 
  29.     </webdiyer:AspNetPager> 

后台代码:

  1. public partial class ListView技巧_Code14 : 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.GetEmployee(1,2); 
  22.         AspNetPager1.RecordCount = da.List.Count; 
  23.         ListView1.DataBind(); 
  24.     } 
  25.  
  26.  
  27.     protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e) 
  28.     { 
  29.         ListView1.DataSource = da.GetEmployee(e.NewPageIndex*2-1, 2); 
  30.         ListView1.DataBind(); 
  31.     } 

模拟数据提供代码:

  1. public class DataAccess 
  2.     public List<Employee> List; 
  3.     public DataAccess() 
  4.     { 
  5.         List = new List<Employee>(); 
  6.         List = new List<Employee>(); 
  7.         Employee e1 = new Employee { ID = 1, Name = "A", Age = 10, Sex = 1 }; 
  8.         Employee e2 = new Employee { ID = 3, Name = "M", Age = 30, Sex = 0 }; 
  9.         Employee e3 = new Employee { ID = 2, Name = "B", Age = 40, Sex = 1 }; 
  10.         Employee e4 = new Employee { ID = 4, Name = "S", Age = 19, Sex = 0 }; 
  11.         Employee e5 = new Employee { ID = 6, Name = "X", Age = 18, Sex = 1 }; 
  12.         Employee e6 = new Employee { ID = 5, Name = "W", Age = 20, Sex = 0 }; 
  13.         List.Add(e1); 
  14.         List.Add(e2); 
  15.         List.Add(e3); 
  16.         List.Add(e4); 
  17.         List.Add(e5); 
  18.         List.Add(e6); 
  19.  
  20.     } 
  21.     public List<Employee> GetEmployee(int startIndex, int rowsCount) 
  22.     { 
  23.         List<Employee> listTemp = new List<Employee>(); 
  24.         for (int i = startIndex; i < startIndex+rowsCount; i++) 
  25.         { 
  26.             listTemp.Add(List[i-1]); 
  27.         } 
  28.         return listTemp; 
  29.     } 

7、内嵌DetailsView控件

前端代码

  1. <asp:ListView ID="ListView1" runat="server" OnSelectedIndexChanging="ListView1_SelectedIndexChanging"> 
  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.                         <asp:LinkButton ID="SelectButton" CommandName="Select" runat="server" Text="Details" /> 
  11.                     </td> 
  12.                     <td> 
  13.                         <%#Eval("ID") %> 
  14.                     </td> 
  15.                     <td> 
  16.                         <asp:Label ID="idLabel" runat="server" Text='<%# Eval("name") %>' /> 
  17.                     </td> 
  18.                     <td> 
  19.                         <asp:Label ID="xLabel" runat="server" Text='<%# Eval("age") %>' /> 
  20.                     </td> 
  21.                 </tr> 
  22.             </ItemTemplate> 
  23.             <SelectedItemTemplate> 
  24.             <tr><td> 
  25.                 <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"> 
  26.  
  27.                 </asp:DetailsView></td></tr> 
  28.             </SelectedItemTemplate> 
  29.         </asp:ListView> 

后台代码:

  1. public partial class ListView技巧_Code15 : 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.  
  20.     private void Bind() 
  21.     { 
  22.         ListView1.DataSource = da.List; 
  23.         ListView1.DataBind(); 
  24.     } 
  25.     protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e) 
  26.     { 
  27.         ListView1.SelectedIndex = e.NewSelectedIndex; 
  28.         
  29.          
  30.         Bind(); 
  31.         DetailsView dv = ListView1.Items[e.NewSelectedIndex].FindControl("DetailsView1"as DetailsView; 
  32.         dv.DataSource = da.List; 
  33.         dv.DataBind(); 
  34.     } 

8、内嵌GridView带修改

前端代码

  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.                     <asp:LinkButton ID="SelectButton" CommandName="Select" runat="server" Text="选择" /> 
  12.                     </td> 
  13.                     <td> 
  14.                         <%#Eval("ID") %> 
  15.                     </td> 
  16.                     <td> 
  17.                         <asp:Label ID="idLabel" runat="server" Text='<%# Eval("name") %>' /> 
  18.                     </td> 
  19.                     <td> 
  20.                         <asp:Label ID="xLabel" runat="server" Text='<%# Eval("age") %>' /> 
  21.                     </td> 
  22.                      
  23.                      
  24.                 </tr> 
  25.             </ItemTemplate> 
  26.             <SelectedItemTemplate> 
  27.                  
  28.             <tr> 
  29.                     <td> 
  30.                       <asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="true" 
  31.                        OnRowEditing="GridView1_Editing"  OnRowUpdating="GridView1_Updating"  
  32.                         OnRowCancelingEdit="GridView1_CancelingEdit" AutoGenerateColumns="false"> 
  33.                         <Columns> 
  34.                         <asp:BoundField DataField="ID" ReadOnly="true" /> 
  35.                         <asp:BoundField DataField="name" /> 
  36.                         <asp:BoundField DataField="Age" /> 
  37.    
  38.                         </Columns> 
  39.                     </asp:GridView> 
  40.                     </td> 
  41.                     
  42.                      
  43.                 </tr> 
  44.             </SelectedItemTemplate> 
  45.        </asp:ListView> 

后台代码

  1. DataAccess da; 
  2.     protected void Page_Load(object sender, EventArgs e) 
  3.     { 
  4.         if (Session["da"] == null
  5.         { 
  6.             da = new DataAccess(); 
  7.             Session["da"] = da; 
  8.         } 
  9.         else 
  10.         { 
  11.             da = Session["da"as DataAccess; 
  12.         } 
  13.         if (!IsPostBack) 
  14.         { 
  15.             Bind(); 
  16.         } 
  17.     } 
  18.  
  19.     private void Bind() 
  20.     { 
  21.         ListView1.DataSource = da.List; 
  22.         ListView1.DataBind(); 
  23.     } 
  24.     protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e) 
  25.     { 
  26.         ListView1.SelectedIndex = e.NewSelectedIndex; 
  27.         Bind(); 
  28.         ViewState["SelectedIndex"] = e.NewSelectedIndex; 
  29.         int selectIndex = (int)ViewState["SelectedIndex"]; 
  30.         GridView gv = (GridView)ListView1.Items[selectIndex].FindControl("GridView1"); 
  31.         gv.DataSource = da.List; 
  32.         gv.DataBind(); 
  33.     } 
  34.     protected void GridView1_Editing(object sender, GridViewEditEventArgs e) 
  35.     { 
  36.         int selectIndex = (int)ViewState["SelectedIndex"]; 
  37.         GridView gv = (GridView)ListView1.Items[selectIndex].FindControl("GridView1"); 
  38.         gv.EditIndex = e.NewEditIndex; 
  39.         gv.DataSource = da.List; 
  40.         gv.DataBind(); 
  41.     } 
  42.     protected void GridView1_Updating(object sender, GridViewUpdateEventArgs e) 
  43.     { 
  44.         int selectIndex = (int)ViewState["SelectedIndex"]; 
  45.         GridView gv = (GridView)ListView1.Items[selectIndex].FindControl("GridView1"); 
  46.         string id=gv.Rows[e.RowIndex].Cells.Text; 
  47.         string name= ((TextBox)gv.Rows[e.RowIndex].Cells.Controls[0]).Text; 
  48.         string Age= ((TextBox)gv.Rows[e.RowIndex].Cells.Controls[0]).Text; 
  49.         Employee employee = da.Find(Convert.ToInt32(id)); 
  50.         employee.Name = name; 
  51.         employee.Age = Convert.ToInt32(Age); 
  52.         gv.EditIndex = -1; 
  53.         gv.DataSource = da.List; 
  54.         gv.DataBind(); 
  55.     } 
  56.     protected void GridView1_CancelingEdit(object sender, GridViewCancelEditEventArgs e) 
  57.     { 
  58.         int selectIndex = (int)ViewState["SelectedIndex"]; 
  59.         GridView gv = (GridView)ListView1.Items[selectIndex].FindControl("GridView1"); 
  60.         gv.EditIndex = -1; 
  61.         gv.DataSource = da.List; 
  62.         gv.DataBind(); 
  63.     } 

模拟数据提供和前边一样这里就不贴了

设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师