DataList分页功能----举例说明

来源:互联网 发布:agv导航算法 编辑:程序博客网 时间:2024/05/16 04:39

设计页面的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>DataList分页功能</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align: center">
        <div align="center">
        <table border="0" style="width: 1px">
            <tr>
                <td nowrap="nowrap" style="width: 949px; height: 19px" bgcolor="gray">
                    <div align="center" style="font-weight: bold; font-size: 15px; font-family: 'Courier New'; color: white;">
                        学生寝室管理系统->留言板</div>
                </td>
            </tr>
            <tr>
                <td style="height: 33px; width: 949px;">
                    <div align="center">
                        <asp:DataList ID="mydatalist" runat="server" style="font-size: 13px">
                            <ItemTemplate>
                                <div align="center">
                                    <div align="center">
                                        <div align="center">
                                            <table border="0" style="width: 810px; height: 293px; font-size: 14px;">
                                                <tr>
                                                    <td style="width: 136px; color: white; font-weight: bold; font-size: 14px; height: 19px;" bgcolor="dimgray" align="center">
                                                        留言标题</td>
                                                    <td align="center" style="width: 1813px; color: white; font-weight: bold; font-size: 14px; height: 19px;" bgcolor="dimgray">
                                                        <%#DataBinder.Eval(Container.DataItem, "title")%></td>
                                                    <td align="left" style="width: 763px; height: 19px;" bgcolor="dimgray">
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="width: 136px; height: 228px;">
                                                        <div align="left">
                                                            <table border="0" style="width: 205px">
                                                                <tr>
                                                                    <td width="216" style="height: 125px">
                                                                        <div align="center">
                                                                            <asp:Image ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"userface","images/userface/{0}.gif") %>' /></div>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td nowrap="nowrap">
                                                                        <div align="left">
                                                                           <font color="#000033"> 姓名:</font><%#DataBinder.Eval(Container.DataItem, "username")%></div>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td nowrap="nowrap">
                                                                        <font color="#000033">身份:</font><%#DataBinder.Eval(Container.DataItem, "usershenfen")%></td>
                                                                </tr>
                                                                <tr>
                                                                    <td nowrap="nowrap" style="height: 21px">
                                                                        <div align="left">
                                                                            <font color="#000033">性别:</font><%#DataBinder.Eval(Container.DataItem, "usersex")%></div>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td nowrap="nowrap">
                                                                        <div align="left">
                                                                           <font color="#000033"> 地址:</font><%#DataBinder.Eval(Container.DataItem, "useraddress")%></div>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td nowrap="nowrap">
                                                                        <div align="left">
                                                                           <font color="#000033"> E-mail:</font><%#DataBinder.Eval(Container.DataItem, "useremail")%></div>
                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </div>
                                                    </td>
                                                    <td style="height: 228px" colspan="2">
                                                        <div align="center">
                                                            <div align="center">
                                                                <div align="center">
                                                                    <div align="left">
                                                                    <asp:Image ID="Image2" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"img","images/emot/{0}.gif") %>' /><br />
                                                                        <%#DataBinder.Eval(Container.DataItem, "content")%></div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td colspan="2" style="height: 19px" align="left">
                                                    <font color="#000033">入学时间:</font><%#DataBinder.Eval(Container.DataItem, "entertime")%> |  <font color="#000033">所在系名:</font><%#DataBinder.Eval(Container.DataItem, "userxiname")%>   |  <font color="#000033">所学专业:</font><%#DataBinder.Eval(Container.DataItem, "userzhuanye")%>  |  <font color="#000033">班级名称:</font><%#DataBinder.Eval(Container.DataItem, "classname")%></td>
                                                    <td style="width: 763px; height: 19px;" align ="left" >
                                                      <font color="#000033">  留言时间:</font><%#DataBinder.Eval(Container.DataItem, "edittime")%> </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                                <div align="center">
                                    <table border="0" style="width: 808px; font-size: 14px;">
                                        <tr>
                                            <td align="left" style="height: 1px">
                                                <div align="center">
                                                    <hr color="gray" />
                                                </div>
                                                 <font color="#000033"> 回复人:</font><%#DataBinder.Eval(Container.DataItem, "rename")%>(管理员)               <font color="#000033">回复时间:</font><%#DataBinder.Eval(Container.DataItem, "replytime")%></td>
                                        </tr>
                                        <tr>
                                            <td nowrap="nowrap">
                                                <div align="left">
                                                      <font color="#000033">回复内容:<asp:Image ID="Image3" runat="server" ImageAlign="AbsMiddle"
                                                          ImageUrl='<%# DataBinder.Eval(Container.DataItem,"replyimg","images/emot/{0}.gif") %>' /></font><%#DataBinder.Eval(Container.DataItem, "reply")%></div>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </ItemTemplate>
                        </asp:DataList></div>
                </td>
            </tr>
            <tr>
                <td nowrap="nowrap" style="height: 24px; width: 949px; font-weight: bold; color: white; background-color: gray;">
                    <div align="center" style="font-size: 14px">
                        <asp:Label ID="Label1" runat="server" Text="每页显示"></asp:Label><asp:Label ID="Label3"
                            runat="server" ForeColor="Red" Style="font-weight: bold" Text="6"></asp:Label><asp:Label
                                ID="Label2" runat="server" Text="条信息 共有"></asp:Label><asp:Label ID="lblRecordCount"
                                    runat="server" Font-Bold="True" ForeColor="Red"></asp:Label><asp:Label ID="Label4"
                                        runat="server" Text="条记录 当前是第"></asp:Label><asp:Label ID="Labelye" runat="server"
                                            Font-Bold="True" ForeColor="Red" Text="1"></asp:Label><asp:Label ID="Label6" runat="server"
                                                Style="font-weight: bold" Text="/"></asp:Label><asp:Label ID="lblPageCount" runat="server"
                                                    Font-Bold="True" ForeColor="Red"></asp:Label><asp:Label ID="Label5" runat="server"
                                                        Text="页"></asp:Label>
                        <asp:Button ID="Button1" runat="server" BackColor="#404040"
                                                            CausesValidation="False" Height="22px" OnClick="Button1_Click" Style="font-weight: bold;
                                                            font-size: 13px; color: white" Text="上一页" Width="59px" />
                        <asp:Button ID="Button2" runat="server" BackColor="#404040" CausesValidation="False"
                            Height="22px" OnClick="Button2_Click" Style="font-weight: bold; font-size: 13px;
                            color: white" Text="下一页" Width="55px" />
                        <asp:Button ID="Button3" runat="server" BackColor="#404040" CausesValidation="False"
                            Height="22px" OnClick="Button3_Click" Style="font-weight: bold; font-size: 13px;
                            color: white" Text="首页" />
                        <asp:Button ID="Button4" runat="server" BackColor="#404040" CausesValidation="False"
                            Height="22px" OnClick="Button4_Click" Style="font-weight: bold; font-size: 13px;
                            color: white" Text="尾页" /></div>
                </td>
            </tr>
        </table>
    </div>
    </div>
    </form>
</body>
</html>

 

代码隐藏页中的源代码:

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;//引进连接Sql Server的命名空间
public partial class _Default : System.Web.UI.Page
{
    SqlCommand cmd,cmd1;
    SqlConnection cn;
    string strsql;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.databindlist();
        }
    }
    public void databindlist()     //申请一个方法来绑定数据
    {
      
        cn = sqldata.createcon();//连接数据库,我把数据库封装到“sqldata”类中了
        cn.Open();         //打开连接
        int countpage = Convert.ToInt32(this.Labelye.Text);  //获取当前页面
        strsql = "select * from guestbook  order by id desc "; //Sql查询语句
        da = new SqlDataAdapter (strsql, cn);
        da.Fill(ds, "Result");        //填充数据
        System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();   //封装数据绑定控件类,与分页相关的属性
        ps.DataSource = ds.Tables["Result"].DefaultView;
        ps.AllowPaging = true;    //获取或实置一个植,指示是否在数据绑定控件中启用分页
        ps.PageSize = 6;     //获取或实置每页显示的条数
        ps.CurrentPageIndex = countpage - 1;  //获取或实置当前页的索引
        this.lblPageCount.Text = Convert.ToString(ps.PageCount);  //统计共有多少页
        this.Button1.Enabled = true;
        this.Button2.Enabled = true;
        if (countpage == 1)
        {
            this.Button1.Enabled = false;//如果当前页等于"1",那么"Button1"不可用
        }
        if (countpage == ps.PageCount)
        {
            this.Button2.Enabled = false;//如果当前页等于"尾页",那么"Button2"不可用
        }
        this.mydatalist.DataSource = ps;
        this.mydatalist.DataBind();//将数据源绑定到DataList
        SqlCommand  cmd1 = new SqlCommand ("select count(*) from guestbook", cn);
        this.lblRecordCount.Text = Convert.ToString(cmd1.ExecuteScalar());//统计共有多少条记录
        sqldata.pagecount = Convert.ToString(ps.PageCount);
        cn.Close();//关闭连接
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.Labelye.Text = Convert.ToString(Convert.ToInt32(this.Labelye.Text) - 1); //获取当前的页数
        this.databindlist();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.Labelye.Text = Convert.ToString(Convert.ToInt32(this.Labelye.Text) + 1); //获取下一页的页数
        this.databindlist();  //绑定数据源
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        this.Labelye.Text = "1"; //获取首页
        this.databindlist(); //绑定数据源
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        this.Labelye.Text =sqldata.pagecount; //获取尾页
        this.databindlist();  //绑定数据源
    }
}