做了一個網站,其中的線上留言板塊是用Repeater來顯示留言的,這樣可以用少的代碼還實現多的功能,但是不知道怎麼分頁,要是留言過多就會使頁面變的很長,能過查看眾多網友的經驗,知道用PagedDataSource來實現。 Repeater分頁,需要依靠PagedDataSource。這個類存在於Sy ...
做了一個網站,其中的線上留言板塊是用Repeater來顯示留言的,這樣可以用少的代碼還實現多的功能,但是不知道怎麼分頁,要是留言過多就會使頁面變的很長,能過查看眾多網友的經驗,知道用PagedDataSource來實現。
Repeater分頁,需要依靠PagedDataSource。這個類存在於System.Web.UI.WebControls命名空間。它的作用是作為數據源與數據顯示控制項的中間介質。如:
數據源->PagedDataSource->數據綁定控制項
之間的關係通過以下代碼來實現:
PagedDataSource pds=new PagedDataSource ();
pds.DataSource=dataTable;
repeater1.DataSource=pds;
repeater1.DataBind();
這是前臺aspx頁代碼
<div id="onlinely"> <div id="xianshily"> <asp:Repeater runat="server" ID="Repeater1"> <ItemTemplate> <ul id="xianshi1"> <li id="xianshi2"> <ul class="ly1"> <li class="ly2">留言人:</li> <li class="ly3"><asp:Label runat="server" ID="lyren" Text='<%#Eval("lyname") %>'></asp:Label></li> <li class="ly4">時間:</li> </ul> <ul class="ly1"> <li class="ly2">留言內容:</li> <li class="ly3"><asp:Label runat="server" ID="lynr" Text='<%#Eval("neirong") %>'></asp:Label></li> <li class="ly4"><asp:Label runat="server" ID="lyt" Text='<%#Eval("lytime") %>'></asp:Label></li> </ul> <ul class="ly1"> <li class="ly2">回覆:</li> <li class="ly3"><asp:Label runat="server" ID="lyhf" Text='<%#Eval("huifu") %>'></asp:Label></li> <li class="ly4"><asp:Label runat="server" ID="hft" Text='<%#Eval ("hftime") %>'></asp:Label></li> </ul> </li></ul> </ItemTemplate> </asp:Repeater> </div> <div id="pages"> <asp:hyperlink id="lnkPrev" runat="server">上頁</asp:hyperlink> <asp:hyperlink id="lnkNext" runat="server">下頁</asp:hyperlink> 第<asp:label id="lblCurrentPage" runat="server"></asp:label>頁 共 <asp:label id="lblTotalPage" runat="server"></asp:label>頁 </div> <div id="ly"> <ul> <li> <ul class="ly5"> <li class="ly6">姓名:</li> <li class="ly7"><asp:TextBox runat="server" ID="lyname"></asp:TextBox></li> </ul> <ul class="ly5"> <li class="ly6">留言內容:</li> <li class="ly7"><asp:TextBox runat="server" ID="lyneirong" TextMode="MultiLine" Wrap="true" Height="100px" Width="300px"></asp:TextBox></li> </ul> </li> </ul> <div id="tijiao"> <ul> <li><asp:Button runat="server" ID="Button1" Text="提交" onclick="tijiao_Click" /></li> <li><asp:Button runat="server" ID="quxiao" Text="取消" onclick="quxiao_Click" /></li> </ul> </div> </div> </div>
這是aspx.cs頁代碼:
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; 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.Common; public partial class onlinely : System.Web.UI.Page { SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["zgbygy"].ConnectionString); protected void Page_Load(object sender, EventArgs e) { conn.Open(); string sql = "select * from liuyan order by lytime desc"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); PagedDataSource pgds=new PagedDataSource(); pgds.DataSource = ds.Tables[0].DefaultView; // 設置允許分頁 pgds.AllowPaging = true; // 每頁顯示為8行 pgds.PageSize = 8; // 顯示總共頁數 lblTotalPage.Text = pgds.PageCount.ToString(); // 當前頁 int CurrentPage; // 請求頁碼為不為null設置當前頁,否則為第一頁 if (Request.QueryString["Page"] != null) { CurrentPage = Convert.ToInt32(Request.QueryString["Page"]); } else { CurrentPage = 1; } // 當前頁所引為頁碼-1 pgds.CurrentPageIndex = CurrentPage - 1; // 顯示當前頁碼 lblCurrentPage.Text = CurrentPage.ToString(); // 如果不是第一頁,通過參數Page設置上一頁為當前頁-1,否則不顯示連接 if (!pgds.IsFirstPage) { // Request.CurrentExecutionFilePath為當前請求虛擬路徑 lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1); } // End If // 如果不是最後一頁,通過參數Page設置下一頁為當前頁+1,否則不顯示連接 if (!pgds.IsLastPage) { // Request.CurrentExecutionFilePath為當前請求虛擬路徑 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage + 1); } Repeater1.DataSource =pgds; Repeater1.DataBind(); cmd.Dispose(); conn.Close(); } protected void tijiao_Click(object sender, EventArgs e) { try { conn.Open(); string sql = "insert into liuyan (lyname,neirong,lytime) values ('" +lyname.Text +"','" +lyneirong.Text + "','" + DateTime.Now + "')"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); this.lyname.Text = ""; this.lyneirong.Text = ""; Response.Redirect("onlinely.aspx"); } catch { } } protected void quxiao_Click(object sender, EventArgs e) { this.lyname.Text = ""; this.lyneirong.Text = ""; } public void xinashi(object sender, EventArgs e) { conn.Open(); string sql = "select * from liuyan"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); Repeater1.DataSource = ds; Repeater1.DataBind(); cmd.Dispose(); conn.Close(); } }