datalist实现分页

来源:互联网 发布:linux下lnmp环境搭建 编辑:程序博客网 时间:2024/06/09 20:15

 原文件test.aspx代码:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!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>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <table border="0" cellpadding="0" cellspacing="0" align="center">
    <tr>
        <td style="text-align: center;">
            价格信息
        </td>
    </tr>
    <tr>
        <td style="text-align: center; height: 139px;">
            <asp:DataList ID="DataList1" runat="server">
                <ItemTemplate>
                    <table border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td style="width: 100px; height: 16px">
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                            </td>
                            <td style="width: 100px; height: 16px">
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("lname") %><%# Bind("fname") %>'>'></asp:Label>
                            </td>
                            <td style="width: 100px; height: 16px">
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("premoney") %>'></asp:Label>
                            </td>
                            <td style="width: 100px; height: 16px">
                                <asp:Button ID="btnEdit" runat="server" Text="修改" OnClientClick="return confirm('确认要编辑吗?');"
                                    CommandName="Edit" CommandArgument=' <%# Eval( "id") %>   ' />
                            </td>
                            <td style="width: 100px; height: 16px">
                                <asp:Button ID="Button2" runat="server" Text="删除" OnClientClick="return confirm('确认要删除吗?');"
                                    CommandArgument='<%#Eval("Id") %>' CommandName="Delete" />
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
                <HeaderTemplate>
                    <table border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td style="width: 100px; height: 12px;">
                                编号
                            </td>
                            <td style="width: 100px; height: 12px;">
                                姓名
                            </td>
                            <td style="width: 100px; height: 12px;">
                                预存款
                            </td>
                            <td style="width: 100px; height: 12px;">
                                编辑
                            </td>
                            <td style="width: 100px; height: 12px">
                                删除
                            </td>
                        </tr>
                    </table>
                </HeaderTemplate>
             </asp:DataList>
            <asp:Label ID="lbCount" runat="server" Text="lbCount"></asp:Label>
            <asp:Label ID="lbTotalPage" runat="server" Text="lbTotalPage"></asp:Label>
            <asp:Label ID="lbCurPage" runat="server" Text="lbCurPage"></asp:Label>
            <asp:HyperLink ID="hlnkHead" runat="server">首页</asp:HyperLink>
            <asp:HyperLink ID="hlnkPrev" runat="server">上一页</asp:HyperLink>
            <asp:HyperLink ID="hlnkNext" runat="server">下一页</asp:HyperLink>
            <asp:HyperLink ID="hlnkEnd" runat="server">尾页</asp:HyperLink>
         </td>
         </tr>
    </table>      
      
    </form>
</body>
</html>

 

test.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;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dListBindData();
    }
    private void dListBindData()
    {
        string strCon ="server=.;uid=sa;pwd=sa;database=northwind";
        SqlConnection con = new SqlConnection(strCon);
        SqlDataAdapter da = new SqlDataAdapter("select * from ren", con);
        DataSet ds = new DataSet();
        try
        {
            con.Open();
            //填充数据
            da.Fill(ds, "ren");
            // 创建分页类
            PagedDataSource objPage = new PagedDataSource();
            objPage.DataSource = ds.Tables["ren"].DefaultView;
            //设置总的查询结果
            lbCount.Text = "总共有" + ds.Tables["ren"].Rows.Count.ToString()
+ "条记录";
            //设置可以分页以及每页的行数
            objPage.AllowPaging = true;
            objPage.PageSize = 6;
            //总页数
            lbTotalPage.Text = "总页数:" + objPage.PageCount.ToString() +
"页";
            //定义变量来保存当前页
            int CurPage;
            //判断是否具有页面跳转的请求
            if (Request.QueryString["Page"] != null)
            {
                CurPage = Convert.ToInt32(Request.QueryString["Page"]);
            }
            else
            {
                CurPage = 1;
            }
            //设置当前页的索引
            objPage.CurrentPageIndex = CurPage - 1;
            lbCurPage.Text = "当前页 : 第" + CurPage.ToString() + "页";
            //如果不是首页
            if (!objPage.IsFirstPage)
            {
                //定义上一页超链接的url为: 当前执行页面的虚拟路径,并传递下一页面的索引值
                hlnkHead.NavigateUrl = Request.CurrentExecutionFilePath +
"?Page=" + Convert.ToString(1);
                hlnkPrev.NavigateUrl = Request.CurrentExecutionFilePath +
"?Page=" + Convert.ToString(CurPage - 1);
            }
            //如果不是最后一页
            if (!objPage.IsLastPage)
            {
                //定义“下一页”超链接url为:当前执行页面的虚拟路径,并传递下一页面的索引值
                hlnkNext.NavigateUrl = Request.CurrentExecutionFilePath +
"?Page=" + Convert.ToString(CurPage + 1);
                hlnkEnd.NavigateUrl = Request.CurrentExecutionFilePath +
"?Page=" + objPage.PageCount.ToString();
            }
            DataList1.DataSource = objPage;
            DataList1.DataBind();
            con.Close();
        }
        catch (Exception error)
        {
            Response.Write(error.ToString());
        }
    }

}

原创粉丝点击