• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NET:PagedDataSource

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
图,
 

代码
1,前台
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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>
    <h1>PagedDataSource对象的应用 ylb, tech 2012/6/14</h1>
        <asp:GridView ID="gvwList" runat="server" Width="838px">
        </asp:GridView>
        <asp:PlaceHolder ID="phPageBar" runat="server">
        <table width="850">
            <tr>
                <td>
                    &nbsp;&nbsp; 共找到<asp:Label ID="lblCount" runat="server" Text="100"></asp:Label>记录,当前第<asp:Label
                        ID="lblCurrent" runat="server" Text="1"></asp:Label>&nbsp;
                </td>
                <td><asp:TextBox ID="txtJump" runat="server" Width="20" Text="1"></asp:TextBox>
                    <asp:RangeValidator ID="rvTxtJump" runat="server" ErrorMessage="页码有误" ForeColor="Red"
                        ControlToValidate="txtJump" Display="Dynamic" MinimumValue="1" Type="Integer"></asp:RangeValidator>
                    /共<asp:Label ID="lblPageSum" runat="server" Text="9"></asp:Label><asp:Button ID="btnJump" runat="server" Text="跳转" OnClick="btnJump_Click" />
                    &nbsp;<asp:Button ID="btnFirstPage" runat="server" Text="首页" OnClick="btnFirstPage_Click" />
                    &nbsp;<asp:Button ID="btnCurrentPage" runat="server" Text="上一页" OnClick="btnCurrentPage_Click" />
                    &nbsp;<asp:Button ID="btnNextPage" runat="server" Text="下一页" OnClick="btnNextPage_Click" />
                    &nbsp;<asp:Button ID="btnEndPage" runat="server" Text="尾页" OnClick="btnEndPage_Click" />
                    &nbsp;<asp:DropDownList ID="dropPageSize" runat="server" Width="40" AutoPostBack="True"
                        OnSelectedIndexChanged="dropPageSize_SelectedIndexChanged">
                        <asp:ListItem>20</asp:ListItem>
                        <asp:ListItem>50</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp
                </td>
            </tr>
        </table>
        </asp:PlaceHolder>
       
    </div>
    </form>
</body>
</html>

 

2,后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    private void BindShow(int currentpage,int pagesize)
    {
        //分页对象
        PagedDataSource pd = new PagedDataSource();
        pd.DataSource = TaoTaoOper.GetAll();   //1,把集合对象写这儿

        #region 分页对象的配置
        lblCount.Text = pd.Count.ToString();    //总记录数
        pd.AllowPaging = true;
        pd.PageSize = pagesize;                 //分页大小
        lblPageSum.Text = pd.PageCount.ToString();      //总页数
        pd.CurrentPageIndex = currentpage - 1;  //设置页码索引
        txtJump.Text = currentpage.ToString();  //当前页
        lblCurrent.Text = currentpage.ToString(); //当前页码
        #endregion

        #region 传统分页
        //首页
        if (currentpage > 1)
            btnFirstPage.Enabled = true;
        else
            btnFirstPage.Enabled = false;

        //上一页
        if (currentpage > 1)
            btnCurrentPage.Enabled = true;
        else
            btnCurrentPage.Enabled = false;

        //下一页
        if (currentpage < pd.PageCount)
            btnNextPage.Enabled = true;
        else
            btnNextPage.Enabled = false;
        //尾页
        if (currentpage < pd.PageCount)
            btnEndPage.Enabled = true;
        else
            btnEndPage.Enabled = false;
        #endregion
        rvTxtJump.MaximumValue = pd.PageCount.ToString(); //跳转页码验证

        //2,GridView控件邦定
        gvwList.DataSource = pd;
        gvwList.DataBind();
    }
  
    #region 分页相关事件
    protected void dropPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小
        int currentpage = 1;                                        //当前页码 重新加载分页邦定

        //调用方法
        BindShow(currentpage, pagesize);
    }
    protected void btnJump_Click(object sender, EventArgs e)
    {
        //跳转页码
        int currentpage =Convert.ToInt32( txtJump.Text);
        int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小

        //分页大小改变
        BindShow(currentpage, pagesize);
    }
    protected void btnFirstPage_Click(object sender, EventArgs e)
    {
        int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小
        //调用方法
        BindShow(1, pagesize);
    }
    protected void btnCurrentPage_Click(object sender, EventArgs e)
    {
        int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小
        int currentpage = Convert.ToInt32(txtJump.Text.Trim())-1;
        //调用方法
        BindShow(currentpage, pagesize);
    }
    protected void btnNextPage_Click(object sender, EventArgs e)
    {
        int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小
        int currentpage = Convert.ToInt32(txtJump.Text.Trim()) + 1;
        //调用方法
        BindShow(currentpage, pagesize);
    }
    protected void btnEndPage_Click(object sender, EventArgs e)
    {
        int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小
        int pageCount = Convert.ToInt32(lblPageSum.Text);
        //调用方法
        BindShow(pageCount, pagesize);
    }
    #endregion


    protected void Page_Load(object sender, EventArgs e)
    {
        #region 分页,Page_Load加载调用
        if (!IsPostBack)
        {
            int pagesize = Convert.ToInt32(dropPageSize.SelectedValue); //分页大小
            int currentpage = 1;//pd.CurrentPageIndex + 1;                  //当前页码

            //调用方法
            BindShow(currentpage, pagesize);
        }
        #endregion

    }

}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Asp.net中FreeTextBoxJS方法代码发布时间:2022-07-10
下一篇:
ASP.NETMVC-PageData的应用发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap