asp:repeater控件使用

简介:

 repeater.aspx:

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="repeater.aspx.cs" Inherits="MyWebservice.repeater" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  4. <html xmlns="http://www.w3.org/1999/xhtml"> 
  5. <head> 
  6. <title>创建一个基本的Repeater控件</title> 
  7. <style type="text/css"> 
  8. *{padding:0;margin:0;} 
  9. .section1,.section2 
  10.     padding:10px 0; 
  11.     color:#369;  
  12. .section2 
  13.     background-color:#f0f6f9; 
  14. </style> 
  15. </head> 
  16. <body> 
  17. <asp:repeater  
  18.     id="repDepts"  
  19.     runat="server"  
  20. > 
  21.     <headertemplate> 
  22.         <h3>下面是所有雇员的一个列表:</h3> 
  23.     </headertemplate> 
  24.     <itemtemplate> 
  25.         <div class="section1"> 
  26.         <%# "<b>部门:</b> " 
  27.             + DataBinder.Eval(Container.DataItem, "ID") 
  28.             + " - "  
  29.             + DataBinder.Eval(Container.DataItem, "FirstName")  
  30.         %> 
  31.         </div> 
  32.     </itemtemplate> 
  33.     <alternatingitemtemplate> 
  34.         <div class="section2"> 
  35.         <%# "<b>部门:</b> " 
  36.             + DataBinder.Eval(Container.DataItem, "ID") 
  37.             + " - "  
  38.             + DataBinder.Eval(Container.DataItem, "FirstName")  
  39.         %> 
  40.         </div> 
  41.     </alternatingitemtemplate> 
  42.     <separatortemplate> 
  43.         <div>--------------------------------------------------</div> 
  44.     </separatortemplate> 
  45.     <footertemplate> 
  46.         <div id="lstFoot">全部记录都已显示.</div> 
  47.     </footertemplate>     
  48. </asp:repeater> 
  49. </body> 
  50. </html> 

repeater.aspx.cs:

 

 
  1. using System; 
  2. using System.Data; 
  3. using System.Data.OleDb; 
  4.  
  5. namespace MyWebservice 
  6.     public partial class repeater : System.Web.UI.Page 
  7.     { 
  8.         protected void Page_Load(object sender, EventArgs e) 
  9.         { 
  10.             if(!IsPostBack) 
  11.             { 
  12.                 OleDbConnection DBConn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("App_Data/EmployeeDatabase.mdb")); 
  13.                 OleDbDataAdapter DBCommand = new OleDbDataAdapter("Select ID, FirstName From Employee Order By FirstName", DBConn); 
  14.                 DataSet ds=new DataSet(); 
  15.                 DBCommand.Fill(ds, "Employee"); 
  16.                 repDepts.DataSource = ds.Tables["Employee"].DefaultView; 
  17.                 repDepts.DataBind(); 
  18.             } 
  19.         } 
  20.     } 

 

图片:

 

参考:

http://www.java2s.com/Code/ASP/ADO.net-Database/BinddatatoaspRepeaterwithitemtemplatealternatingitemtemplateseparatortemplateandfootertemplate.htm

 

当然了可以很轻松的实现导航菜单的结构,repeater是个完全基于模板驱动的一个控件,不生成多余代码,完整靠我们自己来布局,非常自由:

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="repeater.aspx.cs" Inherits="MyWebservice.repeater" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  4. <html xmlns="http://www.w3.org/1999/xhtml"> 
  5. <head> 
  6. <title>创建一个基本的Repeater控件</title> 
  7. <style type="text/css"> 
  8. *{padding:0;margin:0;} 
  9. ul,li{list-style:none;} 
  10. .hMenu 
  11.     background-color:#090; 
  12.     overflow:hidden; 
  13.     width:500px; 
  14.     margin:10px auto; 
  15. .hMenu li 
  16.     float:left; 
  17.     padding:5px; 
  18. </style> 
  19. </head> 
  20. <body> 
  21. <asp:repeater  
  22.     id="repDepts"  
  23.     runat="server"  
  24. > 
  25.     <headertemplate> 
  26.         <ul class="hMenu"> 
  27.     </headertemplate> 
  28.     <itemtemplate> 
  29.         <li> 
  30.         <%# DataBinder.Eval(Container.DataItem, "OpName") %> 
  31.         </li> 
  32.     </itemtemplate> 
  33.     <separatortemplate> 
  34.         <li>|</li> 
  35.     </separatortemplate> 
  36.     <footertemplate> 
  37.         </ul> 
  38.     </footertemplate>     
  39. </asp:repeater> 
  40. </body> 
  41. </html> 

 

也可以实现table表格:

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="repeater.aspx.cs" Inherits="MyWebservice.repeater" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  4. <html xmlns="http://www.w3.org/1999/xhtml"> 
  5. <head> 
  6. <title>创建一个基本的Repeater控件</title> 
  7. <style type="text/css"> 
  8. *{padding:0;margin:0;} 
  9. #myTable 
  10.      
  11. #myTable th,#myTable td 
  12.     border:1px solid #c00;    
  13. #myTable th 
  14. {   background-color:#fc0;} 
  15. #myTable .altCell 
  16. {   background-color:#fff0f0;color:#c00;} 
  17. </style> 
  18. </head> 
  19. <body> 
  20. <asp:repeater  
  21.     id="repDepts"  
  22.     runat="server"  
  23. > 
  24.     <headertemplate> 
  25.         <table id="myTable"> 
  26.         <thead> 
  27.         <tr> 
  28.             <th>标识</th><th>号码</th><th>名称</th> 
  29.         </tr> 
  30.         </thead> 
  31.         <tbody> 
  32.         <tr> 
  33.     </headertemplate> 
  34.     <itemtemplate> 
  35.         <td><%# DataBinder.Eval(Container.DataItem, "OpCode") %></td><td><%# DataBinder.Eval(Container.DataItem, "OpNo") %></td><td><%# DataBinder.Eval(Container.DataItem, "OpName") %></td> 
  36.     </itemtemplate> 
  37.     <AlternatingItemTemplate> 
  38.         <td class="altCell"><%# DataBinder.Eval(Container.DataItem, "OpCode") %></td><td class="altCell"><%# DataBinder.Eval(Container.DataItem, "OpNo") %></td><td class="altCell"><%# DataBinder.Eval(Container.DataItem, "OpName") %></td> 
  39.     </AlternatingItemTemplate> 
  40.     <separatortemplate> 
  41.         </tr><tr> 
  42.     </separatortemplate> 
  43.     <footertemplate> 
           </tr> 



  44.         </tbody> 


  45.  


  46.        </table> 


  47.     </footertemplate>     


  48. </asp:repeater> 


  49. </body> 


  50. </html> 


当然交替也可以这样简单些:

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="repeater.aspx.cs" Inherits="MyWebservice.repeater" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  4. <html xmlns="http://www.w3.org/1999/xhtml"> 
  5. <head> 
  6. <title>创建一个基本的Repeater控件</title> 
  7. <style type="text/css"> 
  8. *{padding:0;margin:0;} 
  9. #myTable 
  10.      
  11. #myTable th,#myTable td 
  12.     border:1px solid #c00;    
  13. #myTable th 
  14. {   background-color:#fc0;} 
  15. #myTable .altRow 
  16. {   background-color:#fff0f0;color:#c00;} 
  17. </style> 
  18. </head> 
  19. <body> 
  20. <asp:repeater  
  21.     id="repDepts"  
  22.     runat="server"  
  23. > 
  24.     <headertemplate> 
  25.         <table id="myTable"> 
  26.         <thead> 
  27.         <tr> 
  28.             <th>标识</th><th>号码</th><th>名称</th> 
  29.         </tr> 
  30.         </thead> 
  31.         <tbody> 
  32.     </headertemplate> 
  33.     <itemtemplate> 
  34.         <tr> 
  35.             <td><%# DataBinder.Eval(Container.DataItem, "OpCode") %></td><td><%# DataBinder.Eval(Container.DataItem, "OpNo") %></td><td><%# DataBinder.Eval(Container.DataItem, "OpName") %></td> 
  36.         </tr> 
  37.     </itemtemplate> 
  38.     <AlternatingItemTemplate> 
  39.         <tr class="altRow"> 
  40.             <td><%# DataBinder.Eval(Container.DataItem, "OpCode") %></td><td><%# DataBinder.Eval(Container.DataItem, "OpNo") %></td><td><%# DataBinder.Eval(Container.DataItem, "OpName") %></td> 
  41.         </tr> 
  42.     </AlternatingItemTemplate> 
  43.     <footertemplate> 
  44.         </tbody> 
  45.        </table> 
  46.     </footertemplate>     
  47. </asp:repeater> 
  48. </body> 
  49. </html> 

 我们可以在每一行的开始加上序号,如下所示:

模板修改如下:

 

 
  1. <itemtemplate> 
  2.     <tr> 
  3.         <td><%# i++ %></td><td><%# DataBinder.Eval(Container.DataItem, "OpCode") %></td><td><%# DataBinder.Eval(Container.DataItem, "OpNo") %></td><td><%# DataBinder.Eval(Container.DataItem, "OpName") %></td> 
  4.     </tr> 
  5. </itemtemplate> 
  6. <AlternatingItemTemplate> 
  7.     <tr class="altRow"> 
  8.         <td><%# i++ %></td><td><%# DataBinder.Eval(Container.DataItem, "OpCode") %></td><td><%# DataBinder.Eval(Container.DataItem, "OpNo") %></td><td><%# DataBinder.Eval(Container.DataItem, "OpName") %></td> 
  9.     </tr> 
  10. </AlternatingItemTemplate> 

后置类中添加如下的保护变量,以便派生的页面可以访问到变量i:

 

 
  1. protected  int i = 1; 

 





 本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/362079,如需转载请自行联系原作者

相关文章
|
3月前
|
SQL 开发框架 前端开发
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
34 0
|
4月前
|
SQL 开发框架 JavaScript
分享33个ASP.NET电子商务源码和40个ASP.NET控件组件源码,总有一款适合您
分享33个ASP.NET电子商务源码和40个ASP.NET控件组件源码,总有一款适合您
32 0
|
5月前
|
开发框架 .NET 数据安全/隐私保护
Asp.Net第二章服务器端控件
Asp.Net第二章服务器端控件
28 0
|
5月前
|
开发框架 JavaScript .NET
Asp.Net就业课之三验证控件
Asp.Net就业课之三验证控件
44 0
|
5月前
|
开发框架 .NET
Asp.Net就业课堂之模板控件
Asp.Net就业课堂之模板控件
42 1
|
11月前
|
开发框架 JavaScript .NET
Asp.net 控件用法汇总-RadioButtonList、DropDownList、button、Checkbox...(续)
Asp.net 控件用法汇总-RadioButtonList、DropDownList、button、Checkbox...(续)
88 0
|
11月前
|
开发框架 JavaScript 前端开发
Asp.net 控件用法汇总-RadioButtonList、DropDownList、button、Checkbox...
Asp.net 控件用法汇总-RadioButtonList、DropDownList、button、Checkbox...
62 0
|
11月前
|
开发框架 .NET C++
【asp.net】控件
【asp.net】控件
69 0
|
12月前
|
开发框架 .NET 数据安全/隐私保护
ASP.NET验证控件合集 含代码演示
ASP.NET验证控件合集 含代码演示
|
SQL 开发框架 .NET
在ASP.NET中使用ListView控件对数据进行显示、分页和排序
在ASP.NET中使用ListView控件对数据进行显示、分页和排序
在ASP.NET中使用ListView控件对数据进行显示、分页和排序