打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
DataList分页

页面源码:

<%@ 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>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="city" runat="server">
            <ItemTemplate>
                id:
                <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>'></asp:Label><br />
                pro_id:
                <asp:Label ID="pro_idLabel" runat="server" Text='<%# Eval("pro_id") %>'></asp:Label><br />
                city:
                <asp:Label ID="cityLabel" runat="server" Text='<%# Eval("city") %>'></asp:Label><br />
                <br />
            </ItemTemplate>
        </asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connsky %>"
            SelectCommand="SELECT city.* FROM city"></asp:SqlDataSource>
         <asp:LinkButton ID="btnFirst" runat="server" OnClick="btnFirst_Click">首页</asp:LinkButton>
         <asp:LinkButton ID="btnPrevious" runat="server" OnClick="btnPrevious_Click">第一页</asp:LinkButton>
         (<asp:Label ID="lblPageIndex" runat="server" Text="Label"></asp:Label>/<asp:Label
            ID="lblPageCount" runat="server" Text="Label"></asp:Label>) <asp:LinkButton
                ID="btnNext" runat="server" OnClick="btnNext_Click">下一页</asp:LinkButton>
      
           <asp:LinkButton ID="btnLast" runat="server" OnClick="btnLast_Click">尾页</asp:LinkButton>
          
   
    </div>
    </form>
</body>
</html>

----------------------------------------------------------------------------------------------------------------------------

.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 _Default : System.Web.UI.Page
{
    //控件开发人员需对自定义数据绑定控件提供分页支持时,可使用的类PagedDatasource
    public static PagedDataSource pds = new PagedDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {

      
        if (!IsPostBack)
        {
            pds.AllowPaging = true;//允许分页
            pds.CurrentPageIndex = 0;//当前页的索引设置为0,即第一页的索引为0
            pds.PageSize = 4;//单页显示的项数

        }
        RefreshStatus();

       
    }
    //流程:1.现在页面加载的时候,把PagedDataSouce的参数设置好(如单页显示多少条记录)
    //2.pds.DataSource=AdminManager.GetAllAdmins();这样就变成有分页的数据了,然后再和DataList绑定就好了
    private void RefreshStatus()
    {
        string strCon ="Data Source=WJW\\SQLEXPRESS;Initial Catalog=cn9sky;User ID=sa;Password=123456";
        SqlConnection con = new SqlConnection(strCon);
       
        SqlDataAdapter da = new SqlDataAdapter("select * from city", con);
        DataSet ds = new DataSet();
        con.Open();
        da.Fill(ds, "city");
        pds.DataSource = ds.Tables["city"].DefaultView;
        city.DataSource = pds;
        city.DataBind();

        btnFirst.Enabled = !pds.IsFirstPage;//判断当前页是不是第一页,如果是,那么"第一页"的按钮就应该不可用

        btnPrevious.Enabled = !pds.IsFirstPage; ;//判断当前页是不是第一页,如果是,那么"上一页"的按钮就应该不可用
        btnNext.Enabled = !pds.IsLastPage;
        btnLast.Enabled = !pds.IsLastPage;

        lblPageIndex.Text = Convert.ToString(pds.CurrentPageIndex + 1);//如果当前页是第一页,pds.CurrentPageIndex的值会为0,所以要+1

        lblPageCount.Text = pds.PageCount.ToString();
    }

    protected void btnFirst_Click(object sender, EventArgs e)
    {
        pds.CurrentPageIndex = 0;//如果是第一页,就把pds.CurrentPageIndex参数设置为0,然后在绑定
        RefreshStatus();
    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        pds.CurrentPageIndex -= 1;
        RefreshStatus();
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        pds.CurrentPageIndex += 1;
        RefreshStatus();
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        pds.CurrentPageIndex = pds.PageCount - 1;
        RefreshStatus(); 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
PageDataSource分页
DataList分页方法
DataList利用PagedDataSource来进行分页!_萧四郎
使用PagedDataSource类实现DataList和Repeater控件的分页显示功...
DataList,PageDataSource打造简单的相册
为datagrid的自带分页添加首页、尾页及状态功能
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服