用Repeater做的留言板的分页功能
来源:互联网 发布:矩阵讲解 编辑:程序博客网 时间:2024/04/29 11:33
做了一个网站,其中的在线留言板块是用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(); } }
- 用Repeater做的留言板的分页功能
- asp.net编程利用Repeater控件做简单的留言板
- Repeater控件分页功能的实现
- repeater的分页和删除功能
- 用dreamweaver在留言板里面做可以添加表情的功能具体怎么做呢?
- repeater分页的实现
- Repeater的分页代码
- repeater分页的实现
- Repeater的分页
- Repeater的分页
- Repeater的分页
- repeater分页的实现
- repeater分页的实现
- Repeater的分页
- 借助于repeater实现自动分页的WEB打印功能
- Repeater控件分页功能的实现 (不推荐)
- 借助于repeater实现自动分页的WEB打印功能
- GridView和Repeater自定义分页功能的实现
- Storm Concept
- phpMyAdmin-3.5.2.2-all-languages.zip的安装配置
- 面试体验:Microsoft 篇
- socket实现多个客户端连接在一个服务器上
- 最有前途的php框架
- 用Repeater做的留言板的分页功能
- setjmp和longjmp的"非本地跳转"
- Spring AOP 详解
- Android平台中应该如何获取音乐文件的信息
- Android中使用自定义Adapter
- (求解)weblogic-ejb-eclipse/toplink配置下的问题
- freemarker记录
- Rich Communication Suite
- kvm 和 qemu的关系