溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

.NET用repeater分頁效果怎么弄

發(fā)布時間:2020-10-16 16:28:04 來源:億速云 閱讀:151 作者:小新 欄目:編程語言

這篇文章主要介紹了.NET用repeater分頁效果怎么弄,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

Repeater控件是一個數(shù)據(jù)綁定容器控件,它能夠生成各個項的列表,并可以使用模板定義網(wǎng)頁上各個項的布局。本文對此進行詳細介紹,下面跟著小編一起來看下吧

Repeater控件是一個數(shù)據(jù)綁定容器控件,它能夠生成各個項的列表,并可以使用模板定義網(wǎng)頁上各個項的布局。當該頁運行時,該控件為數(shù)據(jù)源中的每個項重復(fù)此布局。

配合模板使用repeater控件

若要使用repeater控件,需創(chuàng)建定義控件內(nèi)容布局的模板。模板可以包含標記和控件的任意組合。如果未定義模板,或者模板都不包含元素,則當應(yīng)用程序運行時,該控件不顯示在頁面上。

ItemTemplate : 含要為數(shù)據(jù)源中每個數(shù)據(jù)項都要呈現(xiàn)一次的 HTML 元素和控件。

AlternatingItemTemplate : 對交替數(shù)據(jù)項進行格式設(shè)置(包含要為數(shù)據(jù)源中每個數(shù)據(jù)項都要呈現(xiàn)一次的 HTML 元素和控件。通常,可以使用此模板為交替項創(chuàng)建不同的外觀,例如指定一種與在ItemTemplate中指定的顏色不同的背景色)。

SeparatorTemplate : 對分隔符進行格式設(shè)置(包含在每項之間呈現(xiàn)的元素。)。

HeaderTemplate : 對頁眉進行格式設(shè)置(包含在列表的開始處分別呈現(xiàn)的文本和控件。)。

FooterTemplate : 對頁腳進行格式設(shè)置(包含在列表的結(jié)束處分別呈現(xiàn)的文本和控件。)?!    ?/p>

Repeater分頁效果如下:

前臺代碼:

<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> 
 <p style="margin-left:50px;">
  <p style="margin:0 auto; margin-top:50px;border:1px solid #fff;font-size:16px;font-family:"microsoft yahei","宋體";">
  <a><p 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></p></a>
  <a><p 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></p></a>
  <a><p 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></p></a>
  <a><p 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></p></a>
  <a><p 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></p></a>
  <a><p 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></p></a>
  </p> 
 </p> 
 </body>

后臺代碼:

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;// 設(shè)置允許分頁
  pag.PageSize = 10; // 每頁顯示為3行
  pag.DataSource = list; // 模板綁定數(shù)據(jù)源 
  zong.Text = pag.PageCount.ToString(); // 顯示總共頁數(shù)
  int CurrentPage;
  // 請求頁碼為不為null設(shè)置當前頁,否則為第一頁
  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);
  } 
  // 如果不是最后一頁,通過參數(shù)Page設(shè)置下一頁為當前頁+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();
 }

如果不需要進行分頁,可執(zhí)行以下代碼:

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();
}

感謝你能夠認真閱讀完這篇文章,希望小編分享.NET用repeater分頁效果怎么弄內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學(xué)習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI