• ASP.NET常用的分页


    在显示数据时,常常要用到分页;所以把我经常使用的分页代码记录下来;

    以下分页只适合小型数据!使用的是VS自带的分页功能;

    html页面:

    <div align="right">  
        <asp:LinkButton ID="LbtFirst" runat="server" OnClick="LbtFirst_Click" >首 页</asp:LinkButton>    
        <asp:LinkButton ID="Lbtback" runat="server" OnClick="Lbtback_Click" >上一页</asp:LinkButton>    
        <asp:LinkButton ID="LbtNext" runat="server" OnClick="LbtNext_Click" >下一页</asp:LinkButton>    
        <asp:LinkButton ID="LbtFinally" runat="server" OnClick="LbtFinally_Click">末 页</asp:LinkButton>  
        当前第<asp:Label ID="Lblnum" runat="server" Text="1"></asp:Label>页   
        共<asp:Label ID="lblPageCount" runat="server"></asp:Label>页   
    </div> 
    

    把此代码放入Repeater或者GridView等数据显示控件下面,样式可以自己设一下。

    后台页面:

    static int currentPage = 1;   
    //页面加载   
    protected void Page_Load(object sender, EventArgs e)   
    {    
    	if (!IsPostBack)   
    	{    
    		currentPage = 1;   
    		Bing();   
    	}   
    }   
     //加载数据   
     private  void Bing()   
     {   
         //获取数据   
         IList<Log> log = new Logs().GetAllLog();   
         pageDate(obj);   
     }   
    /// <summary>   
    /// 分页方法,传入要绑定的数据集   
    /// </summary>   
    private void pageDate(IList<Log> obj)   
    {   
        PagedDataSource pds = new PagedDataSource();   
        pds.DataSource = obj;   
        pds.AllowPaging = true;   
        //默认每页显示条数,这里默认为8   
        pds.PageSize = 8;   
        pds.CurrentPageIndex = currentPage - 1;   
        this.Lblnum.Text = currentPage.ToString();   
        this.LbtFirst.Enabled = this.Lbtback.Enabled = !pds.IsFirstPage;   
        this.LbtFinally.Enabled = this.LbtNext.Enabled = !pds.IsLastPage;   
        this.lblPageCount.Text = Convert.ToString(pds.PageCount);   
        ///绑定数据源控件(改成页面数据控件ID)   
        this.GridView.DataSource = pds;   
        this.GridView.DataBind();   
    }   
    /// <summary>   
    /// 第一页   
    /// </summary>   
    protected void LbtFirst_Click(object sender, EventArgs e)   
    {   
        currentPage = 1;   
        Bing()   
    }   
    /// <summary>   
    /// 上一页   
    /// </summary>   
    protected void Lbtback_Click(object sender, EventArgs e)   
    {   
        currentPage -= 1;   
        Bing()   
    }   
    /// <summary>   
    /// 下一页   
    /// </summary>   
    protected void LbtNext_Click(object sender, EventArgs e)   
    {   
        currentPage += 1;   
        Bing()   
    }   
    /// <summary>   
    /// 最后一页   
    /// </summary>   
    protected void LbtFinally_Click(object sender, EventArgs e)   
    {   
        currentPage = Convert.ToInt32(lblPageCount.Text.Trim());   
        Bing()   
    }  
    

    这段代码为后台代码;直接复制粘贴,然后把稍作修改就可以使用了。 

    如果传入的是DataTable 则 pds.DataSource 应改为dataTable.DefaultView;

  • 相关阅读:
    spring 任务调度quartz
    java增强型for循环
    DateTimeFormat
    Java的同步和异步
    HTTP Status 400,400 (Bad Request)
    com.mysql.jdbc.exceptions.jdbc4.MySQLDataException: '2.34435678977654336E17' in column '3' is outside valid range for the datatype INTEGER.
    Servlet.service() for servlet [appServlet] in context with path [/item] threw exception [Request processing failed
    mysql调优
    Windows nexus 启动失败
    NFS客户端访问行为相关的几个参数解释
  • 原文地址:https://www.cnblogs.com/intcry/p/fenyecode1.html
Copyright © 2020-2023  润新知