Repeater
用法:1.绑定数据源: Repeater1.DataSource = context.Info;
Repeater1.DataBind();
2.造模板:
头模板:HeaderTemplate:只出现一次,一般是标题行
脚模板:FooterTemplate:只出现一次
项模板:ItemTemplate:根据数据源中数据的多少生成。
绑定数据:
1.Eval("Code"):绑定显示某一个字段
2.调用函数来显示数据
3.绑定主表中的字段:Nation1.Name
4.格式化显示数据:Eval("Birthday","{0:yyyy年MM月dd日}")
3.如何出现交替项
加一个交替项模板:AlternatingItemTemplate:用来交替显示数据,一般只需要改变样式。
webform web代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <style type="text/css"> * { margin:0px auto; padding:0px; } #menu { list-style:none; } .cd { float:left; width:100px; height:30px; background-color:#FF66CC; font-size:13px; font-family:微软雅黑; text-align:center; line-height:30px; vertical-align:middle; color:white; border-right:1px solid black; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table width="800" border="0" cellspacing="1" cellpadding="1" bgcolor="#6600FF"> <tr> <td width="120" height="30" align="center" valign="middle" bgcolor="#FFFFFF">代号</td> <td width="120" align="center" valign="middle" bgcolor="#FFFFFF">姓名</td> <td width="120" align="center" valign="middle" bgcolor="#FFFFFF">性别</td> <td width="120" align="center" valign="middle" bgcolor="#FFFFFF">民族</td> <td width="200" align="center" valign="middle" bgcolor="#FFFFFF">生日</td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td height="30" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Code") %></td> <td align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Name") %></td> <td align="center" valign="middle" bgcolor="#FFFFFF"><%#ShowSex()%></td> <td align="center" valign="middle" bgcolor="#FFFFFF"><%#ShowNation() %></td> <td align="center" valign="middle" bgcolor="#FFFFFF"><%#ShowBirthday()%></td> </tr> </ItemTemplate> <AlternatingItemTemplate> <tr> <td height="30" align="center" valign="middle" bgcolor="#66CC66"><%#Eval("Code") %></td> <td align="center" valign="middle" bgcolor="#66CC66"><%#Eval("Name") %></td> <td align="center" valign="middle" bgcolor="#66CC66"><%#ShowSex()%></td> <td align="center" valign="middle" bgcolor="#66CC66"><%#ShowNation() %></td> <td align="center" valign="middle" bgcolor="#66CC66"><%#ShowBirthday()%></td> </tr> </AlternatingItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <br /> <asp:Repeater ID="Repeater2" runat="server"> <HeaderTemplate> <u id="menu"> </HeaderTemplate> <FooterTemplate> </u> </FooterTemplate> <ItemTemplate> <li class="cd" bs="<%#Eval("Code") %>"><%#Eval("Name") %></li> </ItemTemplate> </asp:Repeater> </div> </form> </body> </html>后台c#代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { TestDataContext context = new TestDataContext(); Repeater1.DataSource = context.Info; Repeater1.DataBind(); Repeater2.DataSource = context.Nation; Repeater2.DataBind(); } } public string ShowSex() { //return Convert.ToBoolean(Eval("Sex")) ? "男" : "女"; if (Convert.ToBoolean(Eval("Sex"))) { return "<mark>男</mark>"; } else { return "女"; } } public string ShowNation() { string nation = Eval("Nation").ToString(); TestDataContext context = new TestDataContext(); return context.Nation.Where(p=>p.Code == nation).First().Name; } public string ShowBirthday() { return Convert.ToDateTime( Eval("Birthday")).ToString("yyyy年MM月dd日"); } }
时间: 2024-10-19 02:36:05