Repeater控件是一個數據綁定容器控件,它能夠生成各個項的列表,并可以使用模板定義網頁上各個項的布局。當該頁運行時,該控件為數據源中的每個項重復此布局。
配合模板使用repeater控件
若要使用repeater控件,需創建定義控件內容布局的模板。模板可以包含標記和控件的任意組合。如果未定義模板,或者模板都不包含元素,則當應用程序運行時,該控件不顯示在頁面上。
ItemTemplate : 含要為數據源中每個數據項都要呈現一次的 HTML 元素和控件。
AlternatingItemTemplate : 對交替數據項進行格式設置(包含要為數據源中每個數據項都要呈現一次的 HTML 元素和控件。通常,可以使用此模板為交替項創建不同的外觀,例如指定一種與在ItemTemplate中指定的顏色不同的背景色)。
SeparatorTemplate : 對分隔符進行格式設置(包含在每項之間呈現的元素。)。
HeaderTemplate : 對頁眉進行格式設置(包含在列表的開始處分別呈現的文本和控件。)。
FooterTemplate : 對頁腳進行格式設置(包含在列表的結束處分別呈現的文本和控件。)。
Repeater分頁效果如下:
前臺代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
< body > < asp:Repeater ID = "Repeater1" runat = "server" > < HeaderTemplate > < p style = "background-color:#988c6e;width:400px;padding-top:5px;padding-bottom:5px;margin-left:30px;margin-top:30px;border-radius:5px;color:#fff;font-weight:bold;" >< span style = "padding-left:30px;" >用戶名</ span >< span style = "padding-left:100px;" >注冊時間</ span >< span style = "padding-left:90px;" >訪問量</ span ></ p > < table style = "margin-left:30px;margin-top:30px;" > </ HeaderTemplate > < ItemTemplate > < tr > < td style = "width:120px;text-align:left; padding-left:20px;" ><%#Eval("Username") %></ td > < td style = "width:170px;text-align:left; " ><%#Eval("RegistrationTime") %></ td > < td style = "width:50px;text-align:left; " ><%#Eval("AccessAmount") %></ td > </ tr > < tr > < td colspan = "3" style = "border-bottom:1px inset #C0D9D9;padding-top:7px;" ></ td > </ tr > </ ItemTemplate > < FooterTemplate > </ table > </ FooterTemplate > </ asp:Repeater > < div style = "margin-left:50px;" > < div style = "margin:0 auto; margin-top:50px;border:1px solid #fff;font-size:16px;font-family:" microsoft yahei","宋體";"> < a >< div style = "border:1px solid #000; width:60px; float:left; margin:5px;text-align:center;" >< a style = "color:#000" >共< asp:Label runat = "server" ID = "zong" > </ asp:Label >頁</ a ></ div ></ a > < a >< div style = "border:1px solid #000; width:60px; float:left;margin:5px;text-align:center;" >< a style = "color:#000" >第< asp:Label runat = "server" ID = "dangqian" > </ asp:Label >頁</ a ></ div ></ a > < a >< div style = "border:1px solid #000; width:40px; float:left;margin:5px;text-align:center;" > < a style = "color:#000" >< asp:hyperlink id = "first" runat = "server" style = "color:#000" >首頁</ asp:hyperlink ></ a ></ div ></ a > < a >< div style = "border:1px solid #000; width:60px; float:left;margin:5px;text-align:center;" >< a style = "color:#000" >< asp:hyperlink id = "lnkPrev" runat = "server" style = "color:#000" >上一頁</ asp:hyperlink ></ a ></ div ></ a > < a >< div style = "border:1px solid #000; width:60px; float:left;margin:5px;text-align:center;" >< a style = "color:#000" >< asp:hyperlink id = "lnkNext" runat = "server" style = "color:#000" >下一頁</ asp:hyperlink ></ a ></ div ></ a > < a >< div style = "border:1px solid #000; width:40px; float:left;margin:5px;text-align:center;" > < a style = "color:#000" >< asp:hyperlink id = "end" runat = "server" style = "color:#000" >尾頁</ asp:hyperlink ></ a ></ div ></ a > </ div > </ div > </ body > |
后臺代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
protected void Page_Load( object sender, EventArgs e) { if (!Page.IsPostBack) { getUsers(); } } private void getUsers() { List<Users1> list = new AdminManager().QueryUsers(); PagedDataSource pag = new PagedDataSource(); pag.AllowPaging = true ; // 設置允許分頁 pag.PageSize = 10; // 每頁顯示為3行 pag.DataSource = list; // 模板綁定數據源 zong.Text = pag.PageCount.ToString(); // 顯示總共頁數 int CurrentPage; // 請求頁碼為不為null設置當前頁,否則為第一頁 if (Request.QueryString[ "Page" ] != null ) { CurrentPage = Convert.ToInt32(Request.QueryString[ "Page" ]); } else { CurrentPage = 1; } if (Request.QueryString[ "PageSize" ] != null ) { pag.PageSize = Convert.ToInt32(Request.QueryString[ "PageSize" ]); } else { pag.PageSize = 10; } pag.CurrentPageIndex = CurrentPage - 1; // 當前頁所引為頁碼-1 dangqian.Text = CurrentPage.ToString(); // 當前頁 if (!pag.IsFirstPage) { // Request.CurrentExecutionFilePath為當前請求虛擬路徑 lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1); } // 如果不是最后一頁,通過參數Page設置下一頁為當前頁+1,否則不顯示連接 if (!pag.IsLastPage) { // Request.CurrentExecutionFilePath為當前請求虛擬路徑 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage + 1); } //首頁 first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1); //尾頁 end.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + pag.PageCount.ToString(); if (Convert.ToInt32(HttpContext.Current.Request[ "page" ]) > pag.PageCount) { first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1); } this .Repeater1.DataSource = pag; this .Repeater1.DataBind(); } |
如果不需要進行分頁,可執行以下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
protected void Page_Load( object sender, EventArgs e) { if (!Page.IsPostBack) { getUsers(); } } private void getUsers() { List<Users1> list = new AdminManager().QueryUsers(); this .Repeater1.DataSource = list ; this .Repeater1.DataBind(); } |
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://www.cnblogs.com/swjian/p/6361434.html