今天做网站的时候,用到了分页技术,我把使用方法记录下来,以便日后查阅以及帮助新手朋友们。
DataList控件可以按照列表的形式显示数据表中的多行记录,但是被显示的多行记录没有分页功能,使用起来不太方便。因此需要借助PagedDataSource类来实现分页,该类封装了数据控件的分页属性,其常用属性及说明如下表所示。
属 性 | 说 明 |
AllowPaging |
获取或设置是否启用分页 |
AllowCustomPaging | 获取或设置是否启用自定义分页 |
CurrentPageIndex | 获取或设置当前显示页的索引 |
DataSource |
获取或设置用于填充控件中项的源数据 |
PageSize |
获取或设置要在数据绑定控件的每页上显示的项数 |
PageCount |
获取显示数据绑定控件中各项所需的总页数 |
FirstIndexPage |
获取页中的第一个索引 |
IsFirstPage |
获取一个值,该值指示当前页是否是首页 |
IsLastPage |
获取一个值,该值指示当前页是否是最后一页 |
DataKeyField |
获取或设置由DataSource属性指定的数据源中的键字段 |
DataKeys |
获取存储在数据列表控件中的每个记录的键值 |
大致实现效果图如下:
具体代码如下:
前台(aspx)代码:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>DataList实现分页</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.gameaccCode") %>'></asp:Label> <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.gameaccPrice") %>'></asp:Label> <asp:Label ID="Label3" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.gameaccType") %>'></asp:Label> </ItemTemplate> </asp:DataList> <table style="width:450px; padding:0;border-spacing:0; text-align:center" > <tr><td> </td></tr> <tr> <td style="width: 600px; text-align: align; font-size: 9pt; height: 15px;" > <asp:Label ID="labCP" runat="server" Text="当前第"></asp:Label> <asp:Label ID="labPage" runat="server" Text="1"></asp:Label> 页 <asp:Label ID="labTP" runat="server" Text="共"></asp:Label> <asp:Label ID="labBackPage" runat="server"></asp:Label> 页 <asp:LinkButton ID="lnkbtnOne" runat="server" Font-Underline="False" ForeColor="Red" OnClick="lnkbtnOne_Click">首页</asp:LinkButton> <asp:LinkButton ID="lnkbtnUp" runat="server" Font-Underline="False" ForeColor="Red" OnClick="lnkbtnUp_Click">上一页</asp:LinkButton> <asp:TextBox ID="TextBox1" runat="server" Height="16px" Width="16px"></asp:TextBox> <asp:LinkButton ID="LinkButton1" runat="server" Font-Underline="False" ForeColor="Red" OnClick="LinkButton1_Click">跳转</asp:LinkButton> <asp:LinkButton ID="lnkbtnNext" runat="server" Font-Underline="False" ForeColor="Red" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton> <asp:LinkButton ID="lnkbtnBack" runat="server" Font-Underline="False" ForeColor="Red" OnClick="lnkbtnBack_Click">尾页</asp:LinkButton> </td> </tr> </table> </div> </form> </body> </html>
后台aspx.cs代码如下:
using System; using System.Data; 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.SqlClient; SqlComm Sql = new SqlComm(); protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) bind(); } protected void bind() { int curpage; //取得当前页的页码 //使用PagedDataSource类实现DataList控件的分页功能 PagedDataSource ps = new PagedDataSource(); //获取数据集 ps.DataSource = Sql.reDt("select * from GameAcc").DefaultView; ; //是否可以分页 ps.AllowPaging = true; //显示的数量 ps.PageSize = 5; if (!int.TryParse(this.labPage.Text, out curpage)) { //Response.Write("<script>alert('页码格式有误!');</script>"); ps.CurrentPageIndex = 0; } //取得当前页的页码 else if (curpage > ps.PageCount) { //Response.Write("<script>alert('页码超出最大页数!');</script>"); ps.CurrentPageIndex = 0; } else ps.CurrentPageIndex = curpage - 1; this.lnkbtnUp.Enabled = true; this.lnkbtnNext.Enabled = true; this.lnkbtnBack.Enabled = true; this.lnkbtnOne.Enabled = true; if (curpage == 1) { //不显示第一页按钮 this.lnkbtnOne.Enabled = false; //不显示上一页按钮 this.lnkbtnUp.Enabled = false; } if (curpage == ps.PageCount) { //不显示下一页 this.lnkbtnNext.Enabled = false; //不显示最后一页 this.lnkbtnBack.Enabled = false; } //显示分页数量 this.labBackPage.Text = Convert.ToString(ps.PageCount); //重新赋值当前页 this.labPage.Text = (ps.CurrentPageIndex + 1).ToString(); //绑定DataList控件 this.DataList1.DataSource = ps; this.DataList1.DataKeyField = "gameaccId"; this.DataList1.DataBind(); } //第一页 protected void lnkbtnOne_Click(object sender, EventArgs e) { this.labPage.Text = "1"; this.bind(); } //上一页 protected void lnkbtnUp_Click(object sender, EventArgs e) { this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) - 1); this.bind(); } //下一页 protected void lnkbtnNext_Click(object sender, EventArgs e) { this.labPage.Text = Convert.ToString(Convert.ToInt32(this.labPage.Text) + 1); this.bind(); } //最后一页 protected void lnkbtnBack_Click(object sender, EventArgs e) { this.labPage.Text = this.labBackPage.Text; this.bind(); } //搜索 protected void LinkButton1_Click(object sender, EventArgs e) { this.labPage.Text = this.TextBox1.Text; this.bind(); }
SqlComm类代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Security.Cryptography; //MD5加密算法引用命名空间 using System.Configuration; using System.Text; /// <summary> /// SqlComm 的摘要说明 /// </summary> public class SqlComm { public SqlComm() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 在Web.config设置连接字符的请款下获取sqlcon连接对象连接数据库 /// </summary> /// <returns>返回sqlconnection对象(AppSettiongs方式)</returns> public SqlConnection Getcon() { string conStr = ConfigurationManager/*.ConnectionStrings*/.AppSettings["con"].ToString(); SqlConnection con = new SqlConnection(conStr); return con; } /// <summary> /// 执行sql语句 /// </summary> /// <param name="sqlStr">执行的sql语句</param> /// <returns>成功返回1,失败返回2</returns> public int sqlEx(string sqlStr) { SqlConnection con = Getcon(); con.Open(); SqlCommand cmd = new SqlCommand(sqlStr, con); try { cmd.ExecuteNonQuery(); return 1; //执行成功返回1 } catch (Exception ex) { return 0; //执行失败返回2 } finally { con.Dispose(); } } /// <summary> /// 执行sql查询,返回数据表 /// </summary> /// <param name="sqlStr">查询表的语句</param> /// <returns>返回DataTable对象</returns> public DataTable reDt(string sqlStr) { SqlConnection con = Getcon(); SqlDataAdapter sda = new SqlDataAdapter(sqlStr, con); DataSet ds = new DataSet(); sda.Fill(ds); return ds.Tables[0]; } /// <summary> /// 执行sql语句,返回数据记录 /// </summary> /// <param name="sqlStr">查记录语句</param> /// <returns>返回DataReader对象</returns> public SqlDataReader reDr(string sqlStr) { SqlConnection con = Getcon(); con.Open(); SqlCommand cmd = new SqlCommand(sqlStr, con); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); //关闭sqlDataReader对象之后,自动关闭连接 return dr; } /// <summary> /// 对密码进行MD5加密 /// </summary> /// <param name="strPwd"></param> /// <returns></returns> public string GetMD5(string strPwd) { string pwd = ""; MD5 md5 = MD5.Create(); //实例化一个md5对象 byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(strPwd)); //将密码转换成utf8编码的字节码 //将字节码进行md5加密,算出hash值 s.Reverse(); //将结果进行翻转 for (int i = 3; i < s.Length - 1; i++) { pwd = pwd + (s[i] < 198 ? s[i] + 28 : s[i]).ToString("X"); } return pwd; } /// <summary> /// 获取执行网页弹窗的字符串 /// </summary> /// <param name="strMessage">弹窗信息</param> /// <returns>返回执行网页信息弹窗的字符串</returns> public string Alert(string strMessage) { return "<script>alert('" + strMessage + "')</script>"; } /// <summary> /// 指定表根据判定条件判断相关数据是否存在 /// </summary> /// <param name="table">表</param> /// <param name="condit">条件(需【''】包括【""】) 如:commodityName='"1"'</param> /// <returns>存在返回1, 不存在返回0</returns> public int Judge_Yes(string table, string condit) { string sqlStr = "select count(*) from " + table + " where " + condit; if (reDt(sqlStr).Rows[0][0].ToString() != "0") return 1; else return 0; } }View Code
Web.config文件<configuration></configuration>标签下加入:
<appSettings> <add key="con" value="server=服务器名;database=数据库名;Integrated Security=True;"/> </appSettings>
以上就是ASP的DataList分页功能的实现了
参考文章:博客园-小Q的博客