asp.net DataList控件分页代码

简介: ...
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="TEST_DataList" %>

<!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">
    <div>
        <asp:DataList ID="score" runat="server" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="Gainsboro"
            EditItemStyle-BackColor="yellow">
            <HeaderTemplate>
                <table width="100%" border="1">
                    <tr>
                        <td align="center">
                            用户ID
                        </td>
                        <td align="center">
                            用户名
                        </td>
                        <td align="center">
                            密 码
                        </td>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td align="center">
                        <%# DataBinder.Eval(Container.DataItem,"用户ID") %>
                    </td>
                    <td align="center">
                        <%# DataBinder.Eval(Container.DataItem,"用户名") %>
                    </td>
                    <td align="center">
                        <%# DataBinder.Eval(Container.DataItem,"密码") %>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:DataList>
        <asp:LinkButton ID="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick"
            runat="server" />
        <asp:LinkButton ID="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick"
            runat="server" />
        共有<asp:Label ID="lblRecordCount" ForeColor="red" runat="server" />条记录 当前为<asp:Label
            ID="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label ID="lblPageCount"
                ForeColor="red" runat="server" />页
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

public partial class TEST_DataList : System.Web.UI.Page
{
    //分页相关参数
    int PageSize, RecordCount, PageCount, CurrentPage;
    SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=CSDN;Persist Security Info=True;User ID=sa;Password=sa;");
    protected void Page_Load(object sender, EventArgs e)
    {
        //设定PageSize
        PageSize = 10;
        if (!IsPostBack)
        {
            ListBind();
            CurrentPage = 0;
            ViewState["PageIndex"] = 0;

            //计算总共有多少记录
            RecordCount = CalculateRecord();
            lblRecordCount.Text = RecordCount.ToString();

            //计算总共有多少页
            PageCount = RecordCount / PageSize;
            lblPageCount.Text = PageCount.ToString();
            ViewState["PageCount"] = PageCount;
        }
    }

    //计算总共有多少条记录
    public int CalculateRecord()
    {
        int intCount;
        string strCount = "select count(*) as co from SYS_USERINF";
        con.Open();
        SqlCommand cmd = new SqlCommand(strCount, con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            intCount = Int32.Parse(dr["co"].ToString());
        }
        else
        {
            intCount = 0;
        }
        dr.Close();
        con.Close();
        return intCount;
    }

    ICollection CreateSource()
    {
        int StartIndex;
        //设定导入的起终地址
        StartIndex = CurrentPage * PageSize;
        string strSel = "select * from SYS_USERINF";
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter(strSel, con);
        sda.Fill(ds, StartIndex, PageSize, "Score");
        return ds.Tables["Score"].DefaultView;
    }

    public void ListBind()
    {
        score.DataSource = CreateSource();
        score.DataBind();
        lbnNextPage.Enabled = true;
        lbnPrevPage.Enabled = true;
        if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;
        if (CurrentPage == 0) lbnPrevPage.Enabled = false;
        lblCurrentPage.Text = (CurrentPage + 1).ToString();
    }

    public void Page_OnClick(Object sender, CommandEventArgs e)
    {
        CurrentPage = (int)ViewState["PageIndex"];
        PageCount = (int)ViewState["PageCount"];
        string cmd = e.CommandName;
        //判断cmd,以判定翻页方向
        switch (cmd)
        {
            case "next":
                if (CurrentPage < (PageCount - 1)) CurrentPage++;
                break;
            case "prev":
                if (CurrentPage > 0) CurrentPage--;
                break;
        }
        ViewState["PageIndex"] = CurrentPage;
        ListBind();
    }
}

样式没有应用好的css,比较难看,但是分页功能就是如此实现的。上图:



相关文章
|
开发框架 JavaScript .NET
asp.net中条件查询+分页
asp.net中条件查询+分页
|
开发框架 JavaScript 前端开发
震撼!破解 ASP.NET 服务器控件 Button 执行顺序之谜,颠覆你的开发认知!
【8月更文挑战第16天】在ASP.NET开发中,通过Button控件实现先执行JavaScript再触后台处理的需求十分常见。例如,在用户点击按钮前需前端验证或提示,确保操作无误后再传递数据至后台深度处理。此过程可通过设置Button的`OnClientClick`属性调用自定义JavaScript函数完成验证;若验证通过,则继续触发后台事件。此外,结合jQuery也能达到相同效果,利用`__doPostBack`手动触发服务器端事件。这种方式增强了应用的交互性和用户体验。
171 8
|
11月前
|
开发框架 .NET PHP
ASP.NET Web Pages - 添加 Razor 代码
ASP.NET Web Pages 使用 Razor 标记添加服务器端代码,支持 C# 和 Visual Basic。Razor 语法简洁易学,类似于 ASP 和 PHP。例如,在网页中加入 `@DateTime.Now` 可以实时显示当前时间。
|
开发框架 .NET API
一个简单的 ASP.NET Core 依赖注入例子,提高代码的可维护性和可扩展性
一个简单的 ASP.NET Core 依赖注入例子,提高代码的可维护性和可扩展性
183 0
|
开发框架 .NET
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
456 0
|
SQL 开发框架 JavaScript
分享33个ASP.NET电子商务源码和40个ASP.NET控件组件源码,总有一款适合您
分享33个ASP.NET电子商务源码和40个ASP.NET控件组件源码,总有一款适合您
285 0
|
SQL 开发框架 前端开发
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
192 0
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
435 0
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
227 7