做了一个网站,其中的在线留言板块是用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[].DefaultView; // 设置允许分页 pgds.AllowPaging = true; // 每页显示为8行 pgds.PageSize = ; // 显示总共页数 lblTotalPage.Text = pgds.PageCount.ToString(); // 当前页 int CurrentPage; // 请求页码为不为null设置当前页,否则为第一页 if (Request.QueryString["Page"] != null) { CurrentPage = Convert.ToInt32(Request.QueryString["Page"]); } else { CurrentPage = ; } // 当前页所引为页码-1 pgds.CurrentPageIndex = CurrentPage - ; // 显示当前页码 lblCurrentPage.Text = CurrentPage.ToString(); // 如果不是第一页,通过参数Page设置上一页为当前页-1,否则不显示连接 if (!pgds.IsFirstPage) { // Request.CurrentExecutionFilePath为当前请求虚拟路径 lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + ); } // End If // 如果不是最后一页,通过参数Page设置下一页为当前页+1,否则不显示连接 if (!pgds.IsLastPage) { // Request.CurrentExecutionFilePath为当前请求虚拟路径 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + ); } 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(); } }