目录搜索
ASP.NET 基础教程ASP.NET 教程Web Pages - 教程Web Pages - 添加 Razor 代码Web Pages - 页面布局Web Pages - 文件夹Web Pages - 全局页面Web Pages - HTML 表单Web Pages - 对象Web Pages - 文件Web Pages - 帮助器Web Pages - WebGrid 帮助器Web Pages - Chart 帮助器Web Pages - WebMail 帮助器Web Pages - PHPWeb Pages - 发布网站Razor - 标记Web Pages - Database 对象ASP.NET MVCWeb Forms - 教程Web Forms - HTML 页面Web Forms - 服务器控件Web Forms - 事件Web Forms - HTML 表单Web Forms - 维持 ViewStateWeb Forms - TextBox 控件Web Forms - Button 控件Web Forms - 数据绑定Web Forms - ArrayList 对象Web Forms - Hashtable 对象Web Forms - SortedList 对象Web Forms - XML 文件Web Forms - Repeater 控件Web Forms - DataList 控件Web Forms - 数据库连接Web Forms - 母版页Web Forms - 导航ASP.NET RazorRazor - C# 和 VB 代码语法Razor - C# 变量Razor - C# 循环和数组Razor - C# 逻辑条件Razor - VB 变量Razor - VB 循环和数组Razor - VB 逻辑条件WF 参考手册Web Forms - HTML 服务器控件ASP.NET HtmlAnchor 控件ASP.NET HtmlButton 控件ASP.NET HtmlForm 控件ASP.NET HtmlGeneric 控件ASP.NET HtmlImage 控件ASP.NET HtmlInputButton 控件ASP.NET HtmlInputCheckBox 控件ASP.NET HtmlInputFile 控件ASP.NET HtmlInputHidden 控件ASP.NET HtmlInputImage 控件ASP.NET HtmlInputRadioButton 控件ASP.NET HtmlInputText 控件ASP.NET HtmlSelect ControlASP.NET HtmlTable 控件ASP.NET HtmlTableCell 控件ASP.NET HtmlTableRow 控件ASP.NET HtmlTextArea 控件ASP.NET AdRotator 控件ASP.NET AdRotator AdvertisementFile 属性ASP.NET AdRotator Target 属性ASP.NET Button 控件ASP.NET Button CausesValidation 属性ASP.NET Button OnClientClick 属性ASP.NET Button PostBackUrl 属性ASP.NET Button Text 属性ASP.NET Button UseSubmitBehavior 属性ASP.NET Button ValidationGroup 属性ASP.NET Calendar 控件ASP.NET Calendar Caption 属性ASP.NET Calendar CaptionAlign 属性ASP.NET Calendar CellPadding 属性ASP.NET Calendar CellSpacing 属性ASP.NET Calendar DayHeaderStyle 属性ASP.NET Style 控件ASP.NET BackColor 属性ASP.NET BorderColor 属性ASP.NET BorderStyle 属性ASP.NET BorderWidth 属性ASP.NET CssClass 属性ASP.NET Font 属性ASP.NET ForeColor 属性ASP.NET Height 属性ASP.NET Width 属性ASP.NET Calendar DayNameFormat 属性ASP.NET Calendar DayStyle 属性ASP.NET Calendar FirstDayOfWeek 属性ASP.NET Calendar NextMonthText 属性ASP.NET Calendar NextPrevFormat 属性ASP.NET Calendar NextPrevStyle 属性ASP.NET Calendar OtherMonthDayStyle 属性ASP.NET Calendar PrevMonthText 属性ASP.NET Calendar SelectedDate 属性ASP.NET Calendar SelectedDates 属性ASP.NET Calendar SelectedDayStyle 属性ASP.NET Calendar SelectionMode 属性ASP.NET Calendar SelectMonthText 属性ASP.NET Calendar SelectorStyle 属性ASP.NET Calendar SelectWeekText 属性ASP.NET Calendar ShowDayHeader 属性ASP.NET Calendar ShowGridLines 属性ASP.NET Calendar ShowNextPrevMonth 属性ASP.NET Calendar ShowTitle 属性ASP.NET Calendar TitleFormat 属性ASP.NET Calendar TitleStyle 属性ASP.NET Calendar TodayDayStyle 属性ASP.NET Calendar TodaysDate 属性ASP.NET Calendar VisibleDate 属性ASP.NET Calendar WeekendDayStyle 属性ASP.NET CalendarDay 控件ASP.NET CalendarDay Date 属性ASP.NET CalendarDay DayNumberText 属性ASP.NET CalendarDay IsOtherMonth 属性ASP.NET CalendarDay IsSelectable 属性ASP.NET CalendarDay IsSelected 属性ASP.NET CalendarDay IsToday 属性ASP.NET CalendarDay IsWeekend 属性ASP.NET CheckBox 控件ASP.NET CheckBox Checked 属性ASP.NET CheckBox Text 属性ASP.NET CheckBox TextAlign 属性ASP.NET CheckBoxList 控件ASP.NET CheckBoxList CellPadding 属性ASP.NET CheckBoxList CellSpacing 属性ASP.NET CheckBoxList RepeatColumns 属性ASP.NET CheckBoxList RepeatDirection 属性ASP.NET CheckBoxList RepeatLayout 属性ASP.NET CheckBoxList TextAlign 属性ASP.NET DropDownList 控件ASP.NET DropDownList SelectedIndex 属性ASP.NET HyperLink 控件ASP.NET HyperLink ImageUrl 属性ASP.NET HyperLink NavigateUrl 属性ASP.NET HyperLink Target 属性ASP.NET HyperLink Text 属性ASP.NET Image 控件ASP.NET Image AlternateText 属性ASP.NET Image ImageAlign 属性ASP.NET Image ImageUrl 属性ASP.NET ImageButton 控件ASP.NET ImageButton CausesValidation 属性ASP.NET ImageButton ValidationGroup 属性ASP.NET ImageButton PostBackUrl 属性ASP.NET ImageButton ValidationGroup 属性ASP.NET Label 控件ASP.NET LinkButton 控件ASP.NET LinkButton OnClientClick 属性ASP.NET LinkButton PostBackUrl 属性ASP.NET LinkButton Text 属性ASP.NET LinkButton ValidationGroup 属性ASP.NET ListBox 控件ASP.NET Rows 属性ASP.NET SelectionMode 属性ASP.NET ListItem 控件ASP.NET ListItem Enabled 属性ASP.NET ListItem Selected 属性ASP.NET ListItem Text 属性ASP.NET ListItem Value 属性ASP.NET Literal 控件ASP.NET Literal Text 属性ASP.NET Panel 控件ASP.NET Panel BackImageUrl 属性ASP.NET Panel DefaultButton 属性ASP.NET Panel Direction 属性ASP.NET Panel GroupingText 属性ASP.NET Panel HorizontalAlign 属性ASP.NET Panel ScrollBars 属性ASP.NET Panel Wrap 属性ASP.NET PlaceHolder 控件ASP.NET RadioButton 控件ASP.NET RadioButtonList 控件ASP.NET RadioButtonList CellPadding 属性ASP.NET RadioButtonList CellSpacing 属性ASP.NET RadioButtonList RepeatColumns 属性ASP.NET RadioButtonList RepeatDirection 属性ASP.NET RadioButtonList TextAlign 属性ASP.NET BulletedList BulletImageUrl 属性Web Forms - Web 服务器控件ASP.NET Table 控件ASP.NET BackImageUrl 属性ASP.NET Caption 属性ASP.NET CaptionAlign 属性ASP.NET CellPadding 属性ASP.NET CellSpacing 属性ASP.NET GridLines 属性ASP.NET HorizontalAlign 属性ASP.NET TableCell 控件ASP.NET TableCell ColumnSpan 属性ASP.NET TableCell HorizontalAlign 属性ASP.NET TableCell RowSpan 属性ASP.NET TableCell Text 属性ASP.NET TableCell Wrap 属性ASP.NET TableRow 控件ASP.NET TableRow HorizontalAlign 属性ASP.NET TableRow TableSection 属性ASP.NET TableRow VerticalAlign 属性ASP.NET TextBox 控件ASP.NET TextBox AutoCompleteType 属性ASP.NET TextBox AutoPostBack 属性ASP.NET TextBox Columns 属性ASP.NET TextBox MaxLength 属性ASP.NET TextBox ReadOnly 属性ASP.NET TextBox Rows 属性ASP.NET TextBox Text 属性ASP.NET TextBox TextMode 属性ASP.NET TextBox Wrap 属性ASP.NET XML 控件ASP.NET XML DocumentSource 属性Web Forms - Validation 服务器控件ASP.NET CompareValidator 控件ASP.NET CustomValidator 控件ASP.NET RangeValidator 控件ASP.NET RegularExpressionValidator 控件ASP.NET RequiredFieldValidator 控件ASP.NET ValidationSummary 控件ASP.NET BulletedList 控件ASP.NET BulletedList BulletStyle 属性ASP.NET BulletedList DisplayMode 属性ASP.NET BulletedList FirstBulletNumber 属性ASP.NET BulletedList Target 属性WP 参考手册Web Pages - 类Web Pages - WebSecurity 对象WebSecurity 属性 - CurrentUserIdWebSecurity 属性 -?CurrentUserNameWebSecurity 属性 - HasUserIdWebSecurity 属性 - IsAuthenticatedWebSecurity - ChangePassword()WebSecurity - ConfirmAccount()WebSecurity - CreateAccount()WebSecurity - CreateUserAndAccount()WebSecurity - GeneratePasswordResetToken()WebSecurity - GetCreateDate()WebSecurity - GetPasswordChangeDate()WebSecurity - GetUserId()WebSecurity - InitializeDatabaseConnection()WebSecurity - IsConfirmed()WebSecurity - IsCurrentUser()WebSecurity - Login()WebSecurity - Logout()WebSecurity - RequireAuthenticatedUser()WebSecurity - RequireRoles()WebSecurity - RequireUser()WebSecurity - ResetPassword()WebSecurity - UserExists()Web Pages - WebMail 对象Web Pages - 更多帮助器
文字

Web Forms - Repeater 控件



Repeater 控件用于显示被绑定在该控件上的项目的重复列表。


绑定 DataSet 到 Repeater 控件

Repeater 控件用于显示被绑定在该控件上的项目的重复列表。Repeater 控件可被绑定到数据库表、XML 文件或者其他项目列表。在这里,我们将演示如何绑定 XML 文件到 Repeater 控件。

在我们的实例中,我们将使用下面的 XML 文件("cdcatalog.xml"):

<?xml version="1.0" encoding="ISO-8859-1"?>

<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
</catalog>

首先,导入 "System.Data" 命名空间。我们需要该命名空间与 DataSet 对象一起工作。 把下面这条指令包含在 .aspx 页面的顶部:

<%@ Import Namespace="System.Data" %>

接着,为 XML 文件创建一个 DataSet,并在页面第一次加载时把这个 XML 文件载入 DataSet:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
end if
end sub

然后我们在 .aspx 页面中创建一个 Repeater 控件。<HeaderTemplate> 元素中的内容被首先呈现,并且在输出中仅出现一次,而 <ItemTemplate> 元素中的内容会对应 DataSet 中的每条 "record" 重复出现,最后,<FooterTemplate> 元素中的内容在输出中仅出现一次:

<html>
<body>

<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
...
</HeaderTemplate>

<ItemTemplate>
...
</ItemTemplate>

<FooterTemplate>
...
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

然后我们添加创建 DataSet 的脚本,并且绑定 mycdcatalog DataSet 到 Repeater 控件。然后 使用 HTML 标签来填充 Repeater 控件,并通过 <%#Container.DataItem("fieldname")%> 绑定数据项目到 <ItemTemplate> 区域内的单元格中:

实例

<%@ Import Namespace="System.Data" %>

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
cdcatalog.DataSource=mycdcatalog
cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

使用 <AlternatingItemTemplate>

您可以在 <ItemTemplate> 元素后添加 <AlternatingItemTemplate> 元素,用来描述输出中交替行的外观。在下面的实例中,表格每隔一行就会显示为浅灰色的背景:

实例

<%@ Import Namespace="System.Data" %>

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
cdcatalog.DataSource=mycdcatalog
cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>

<AlternatingItemTemplate>
<tr bgcolor="#e8e8e8">
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</AlternatingItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

使用 <SeparatorTemplate>

<SeparatorTemplate> 元素用于描述每个记录之间的分隔符。在下面的实例中,每个表格行之间插入了一条水平线:

实例

<%@ Import Namespace="System.Data" %>

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
cdcatalog.DataSource=mycdcatalog
cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
<table border="0" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>

<SeparatorTemplate>
<tr>
<td colspan="6"><hr /></td>
</tr>
</SeparatorTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

上一篇:下一篇: