博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataList怎么分页
阅读量:5154 次
发布时间:2019-06-13

本文共 2246 字,大约阅读时间需要 7 分钟。

在 DataList的页脚那里加上按钮

 

 

 

protected static PagedDataSource ps = new PagedDataSource();//调用分页

 public void Bind(int CurrentPage)

    {

            ps.DataSource = myDs.Tables["CJB"].DefaultView;

            ps.AllowPaging = true;
            ps.PageSize = 5;//显示多少行
            ps.CurrentPageIndex = CurrentPage;
            DataList1.DataSource = ps;
            DataList1.DataKeyField = "学号";
            DataList1.DataBind();

}

 

 

 

 

 

 

 下面代码是在DataList的ItemCommand事件里打的

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)

    {
        switch (e.CommandName)
        {
            case "first":这里是按钮的名字
                Label4.Text = "first";
                ps.CurrentPageIndex = 0;
                Bind(ps.CurrentPageIndex);
                break;
            case "pre":这里是按钮的名字
                ps.CurrentPageIndex = ps.CurrentPageIndex - 1;
                Bind(ps.CurrentPageIndex);
                break;

            case "next":这里是按钮的名字

                ps.CurrentPageIndex = ps.CurrentPageIndex + 1;
                Bind(ps.CurrentPageIndex);
                break;
            case "last":这里是按钮的名字
                ps.CurrentPageIndex = ps.PageCount - 1;
                Bind(ps.CurrentPageIndex);
                break;
            case "search":这里是按钮的名字
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    int PageCount = int.Parse(ps.PageCount.ToString());
                    TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;
                    int MypageNu = 0;
                    if (!txtPage.Text.Equals(""))
                    {
                        MypageNu = Convert.ToInt32(txtPage.Text.ToString());
                    }
                    if (MypageNu <= 0 || MypageNu > PageCount)
                    {
                        Response.Write("<script>alert('请输入页数确定没有超过总页数!')</script>");
                    }
                        else {
                        Bind(MypageNu -1);
                        }
                }
                break;
           
           
        }
       
    }

下面代码是在ItemDataBound事件里的

主要功能是判断

    if(DataList1里没有数据)

    {

    把分页按钮隐藏起来的代码

    }

    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)

    {
        if (e.Item.ItemType == ListItemType.Footer){
        Label CurrentPage = e.Item.FindControl("labNowPage") as Label;
        Label PageCount = e.Item.FindControl("labCount") as Label;
        LinkButton FirstPage = e.Item.FindControl("lnkbtnFirst") as LinkButton;
        LinkButton PrePage = e.Item.FindControl("lnkbtnFront") as LinkButton;
        LinkButton NextPage = e.Item.FindControl("lnkbtnNext") as LinkButton;
        LinkButton LastPage = e.Item.FindControl("lnkbtnLast") as LinkButton;
        CurrentPage.Text = (ps.CurrentPageIndex + 1).ToString();
        PageCount.Text = ps.PageCount.ToString();
        if (ps.IsFirstPage)
        {
            FirstPage.Enabled = false;
            PrePage.Enabled = false;
        }
        if (ps.IsLastPage)
        {
            NextPage.Enabled = false;
            LastPage.Enabled = false;
        }
     }
   }

 这样就可以达到分页功能了

 

转载于:https://www.cnblogs.com/huangyihua/archive/2012/12/25/2832877.html

你可能感兴趣的文章
Python-常用模块及简单的案列
查看>>
(VC/MFC)多线程(Multi-Threading) -1. 基本概念.
查看>>
快数据时代下,Moka携手DataPipeline提升招聘效能
查看>>
day1 用户登陆三次机会
查看>>
LeetCode 159. Longest Substring with At Most Two Distinct Characters
查看>>
LeetCode Ones and Zeroes
查看>>
基本算法概论
查看>>
jquery动态移除/增加onclick属性详解
查看>>
第九周作业
查看>>
MiniMagick
查看>>
css important
查看>>
KindEditor图片上传到七牛云
查看>>
JavaScript---Promise
查看>>
暖暖的感动
查看>>
[转] C语言的谜题
查看>>
Java中的日期和时间
查看>>
禁用windows2000.2003启动时的CTRL+ALT+DEL
查看>>
Django基于admin的stark组件创建(一)
查看>>
快速幂 模板及应用
查看>>
批处理/DOS命令删除文件夹下某类型的文件
查看>>