模板属性 | 说明 |
ItemTemplate | 包含要为数据源中每个数据项都要呈现一次的HTML元素和控件。 |
AlternatingItemTemplate | 包含要为数据源中每个数据项都要呈现一次的HTML元素和控件。通常可以用此模板为交替项创建不同的外观,例如指定一种在ItemTemplate中指定的颜色不同的背景色 |
HeaderTemplate和FooterTemplate | 包含在列表的开始和结束处分别呈现的文本和控件 |
SeparatorTemplate | 包含在每项之间呈现的元素。典型的示例是一条直线(使用hr元素) |
<
asp:Repeater ID
=
"
Repeater1
"
runat
=
"
server
"
DataSourceID
=
"
SqlDataSource1
"
>
< HeaderTemplate >
< table >
< tr >
< td >
我是头模板
</ td ></ tr >
</ HeaderTemplate >
< ItemTemplate >
< tr >
< td >
<% # Eval( " au_lname " ) %>
</ td >
</ tr >
</ ItemTemplate >
< AlternatingItemTemplate >
< tr >< td >
< a href = " default.aspx?id=<%# Eval ( " au_id " ) %>></a> " < font color = " red " ><% # Eval( " au_lname " ) %></ font ></ a >
</ td ></ tr >
</ AlternatingItemTemplate >
< SeparatorTemplate >
< tr >< td >
< hr size = " 1pt " />
</ td ></ tr >
</ SeparatorTemplate >
< FooterTemplate >
</ table >
</ FooterTemplate >
</ asp:Repeater >
< asp:SqlDataSource ID = " SqlDataSource1 " runat = " server " ConnectionString = " <%$ ConnectionStrings:pubconn %> "
SelectCommand = " SELECT [au_id], [au_lname] FROM [authors] " ></ asp:SqlDataSource >
< HeaderTemplate >
< table >
< tr >
< td >
我是头模板
</ td ></ tr >
</ HeaderTemplate >
< ItemTemplate >
< tr >
< td >
<% # Eval( " au_lname " ) %>
</ td >
</ tr >
</ ItemTemplate >
< AlternatingItemTemplate >
< tr >< td >
< a href = " default.aspx?id=<%# Eval ( " au_id " ) %>></a> " < font color = " red " ><% # Eval( " au_lname " ) %></ font ></ a >
</ td ></ tr >
</ AlternatingItemTemplate >
< SeparatorTemplate >
< tr >< td >
< hr size = " 1pt " />
</ td ></ tr >
</ SeparatorTemplate >
< FooterTemplate >
</ table >
</ FooterTemplate >
</ asp:Repeater >
< asp:SqlDataSource ID = " SqlDataSource1 " runat = " server " ConnectionString = " <%$ ConnectionStrings:pubconn %> "
SelectCommand = " SELECT [au_id], [au_lname] FROM [authors] " ></ asp:SqlDataSource >
Repeter实现分页显示,嵌套
using
System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Repeater1.DataSource = pds();
Repeater1.DataBind();
}
ds.Relations.Add("mydb", ds.Tables["name"].Columns["au_id"], ds.Tables["title"].Columns["au_id"]);
}
public PagedDataSource pds()
{
string connstring = ConfigurationManager.ConnectionStrings["pubconn"].ConnectionString;
SqlConnection con = new SqlConnection(connstring);
SqlDataAdapter sda = new SqlDataAdapter("select * from authors", con);
DataSet ds = new DataSet();
sda.Fill(ds, "name");
SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor", con);
sda2.Fill(ds, "title");
ds.Relations.Add("mydb", ds.Tables["name"].Columns["au_id"], ds.Tables["title"].Columns["au_id"]);
PagedDataSource pds = new PagedDataSource();
pds.DataSource =ds.Tables["name"].DefaultView;
pds.AllowPaging = true;
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
pds.PageSize = 5;
return pds;
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
int n = pds().PageCount;
int i = pds().CurrentPageIndex;
Label gjy = (Label)e.Item.FindControl("gjy");
gjy.Text = n.ToString();
Label djy = (Label)e.Item.FindControl("djy");
djy.Text = Convert.ToString(pds().CurrentPageIndex+1);
HyperLink first = (HyperLink)e.Item.FindControl("first");
first.NavigateUrl = "?page=0";
HyperLink end = (HyperLink)e.Item.FindControl("end");
end.NavigateUrl = "?page=" + Convert.ToInt32(n - 1);
HyperLink p = (HyperLink)e.Item.FindControl("p");
HyperLink next = (HyperLink)e.Item.FindControl("next");
if (i <= 0)
{
first.Enabled = false;
p.Enabled = false;
next.Enabled = true;
}
else
{
p.NavigateUrl = "?page=" + Convert.ToInt32(i - 1);
}
if (i > n - 2)
{
end.Enabled = false;
next.Enabled = false;
p.Enabled = true;
}
else
{
next.NavigateUrl = "?page=" + Convert.ToInt32(i + 1);
}
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Repeater1.DataSource = pds();
Repeater1.DataBind();
}
ds.Relations.Add("mydb", ds.Tables["name"].Columns["au_id"], ds.Tables["title"].Columns["au_id"]);
}
public PagedDataSource pds()
{
string connstring = ConfigurationManager.ConnectionStrings["pubconn"].ConnectionString;
SqlConnection con = new SqlConnection(connstring);
SqlDataAdapter sda = new SqlDataAdapter("select * from authors", con);
DataSet ds = new DataSet();
sda.Fill(ds, "name");
SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor", con);
sda2.Fill(ds, "title");
ds.Relations.Add("mydb", ds.Tables["name"].Columns["au_id"], ds.Tables["title"].Columns["au_id"]);
PagedDataSource pds = new PagedDataSource();
pds.DataSource =ds.Tables["name"].DefaultView;
pds.AllowPaging = true;
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
pds.PageSize = 5;
return pds;
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
int n = pds().PageCount;
int i = pds().CurrentPageIndex;
Label gjy = (Label)e.Item.FindControl("gjy");
gjy.Text = n.ToString();
Label djy = (Label)e.Item.FindControl("djy");
djy.Text = Convert.ToString(pds().CurrentPageIndex+1);
HyperLink first = (HyperLink)e.Item.FindControl("first");
first.NavigateUrl = "?page=0";
HyperLink end = (HyperLink)e.Item.FindControl("end");
end.NavigateUrl = "?page=" + Convert.ToInt32(n - 1);
HyperLink p = (HyperLink)e.Item.FindControl("p");
HyperLink next = (HyperLink)e.Item.FindControl("next");
if (i <= 0)
{
first.Enabled = false;
p.Enabled = false;
next.Enabled = true;
}
else
{
p.NavigateUrl = "?page=" + Convert.ToInt32(i - 1);
}
if (i > n - 2)
{
end.Enabled = false;
next.Enabled = false;
p.Enabled = true;
}
else
{
next.NavigateUrl = "?page=" + Convert.ToInt32(i + 1);
}
}
}
}
<
asp:Repeater ID
=
"
Repeater1
"
runat
=
"
server
"
OnItemDataBound
=
"
Repeater1_ItemDataBound
"
>
< HeaderTemplate >
< table width = " 500 " >
< tr style = " background-color:#ccffcc " ; >
< td > 作者 </ td >
< td > 书籍 </ td >
</ tr >
</ HeaderTemplate >
< ItemTemplate >
< tr >
< td ><% # Eval( " au_lname " ) %></ td >
< td >
< asp:Repeater id = " repeter2 " runat = " server " DataSource = ' <%# Eval("mydb") %> ' >
< ItemTemplate >
<% # Eval( " title_id " ) %>
</ ItemTemplate >
</ asp:Repeater >
</ td >
</ tr >
</ ItemTemplate >
< SeparatorTemplate >< td colspan = " 2 " >< hr size = " 1pt " ></ td ></ SeparatorTemplate >
< FooterTemplate >< tr >< td colspan = " 2 " style = " background-color:#ccffcc;font-size:12pt " >
共 < asp:Label ID = " gjy " runat = " server " Text = " Label " ></ asp:Label > 页 当前为第 < asp:Label ID = " djy " runat = " server " Text = " Label " ></ asp:Label > 页
< asp:HyperLink ID = " first " runat = " server " > 首页 </ asp:HyperLink >
< asp:HyperLink ID = " p " runat = " server " > 上一页 </ asp:HyperLink >
< asp:HyperLink ID = " next " runat = " server " > 下一页 </ asp:HyperLink >
< asp:HyperLink ID = " end " runat = " server " > 末页 </ asp:HyperLink >
</ td ></ tr ></ table ></ FooterTemplate >
</ asp:Repeater >
< HeaderTemplate >
< table width = " 500 " >
< tr style = " background-color:#ccffcc " ; >
< td > 作者 </ td >
< td > 书籍 </ td >
</ tr >
</ HeaderTemplate >
< ItemTemplate >
< tr >
< td ><% # Eval( " au_lname " ) %></ td >
< td >
< asp:Repeater id = " repeter2 " runat = " server " DataSource = ' <%# Eval("mydb") %> ' >
< ItemTemplate >
<% # Eval( " title_id " ) %>
</ ItemTemplate >
</ asp:Repeater >
</ td >
</ tr >
</ ItemTemplate >
< SeparatorTemplate >< td colspan = " 2 " >< hr size = " 1pt " ></ td ></ SeparatorTemplate >
< FooterTemplate >< tr >< td colspan = " 2 " style = " background-color:#ccffcc;font-size:12pt " >
共 < asp:Label ID = " gjy " runat = " server " Text = " Label " ></ asp:Label > 页 当前为第 < asp:Label ID = " djy " runat = " server " Text = " Label " ></ asp:Label > 页
< asp:HyperLink ID = " first " runat = " server " > 首页 </ asp:HyperLink >
< asp:HyperLink ID = " p " runat = " server " > 上一页 </ asp:HyperLink >
< asp:HyperLink ID = " next " runat = " server " > 下一页 </ asp:HyperLink >
< asp:HyperLink ID = " end " runat = " server " > 末页 </ asp:HyperLink >
</ td ></ tr ></ table ></ FooterTemplate >
</ asp:Repeater >