asp.net--DataList控件的分页

1.前段代码

 <asp:DataList ID="DataList1" runat="server"  >
                            <ItemTemplate>
                                <li class=Sub>
                                    <a href="Leave.aspx?Name=<%#Eval("Uid")%>"><span><%#Eval("Uid") %></span></a>
                                    <ul class=list-unstyled>
                                        <li>
                                            <a href="ShowWords.aspx?subjectID=<%#Eval("ID")%>">
                                                <%#Eval("Subject")%><!--绑定数据-->
                                            </a>
                                        </li>
                                        <li class=DateTime>
                                            <%#Eval("leaveTime") %>
                                        </li>
                                    </ul>
                                </li>
                            </ItemTemplate>
                            <FooterTemplate >
                                <br />
                                
                                <div id="footer">
                                    <asp:Label ID="labPageCount" runat="server" style="font-weight:bold;" Text="Label"></asp:Label>/
                                    <asp:Label ID="labCurrentPage" runat="server" Text="Label"></asp:Label>
                                    <asp:Button ID="lnkbtnFirst" class="btn btn-default" runat="server" CommandName="first" Text="首页"></asp:Button>
                                    <asp:Button ID="lnkbtnFront" class="btn btn-default" runat="server" CommandName="pre" Text="<< 上一页"></asp:Button>
                                    <asp:Button ID="lnkbtnNext" class="btn btn-default" runat="server" CommandName="next" Text="下一页 >>"></asp:Button>
                                    <asp:Button ID="lnkbtnLast" class="btn btn-default" runat="server" CommandName="last" Text="尾页"></asp:Button>
                                    <span>跳转至:</span>
                                    <asp:TextBox ID="txtPage" class="form-control" runat="server" style="width:40px;display:inline;"></asp:TextBox>
                                    <asp:LinkButton ID="lnkbtnSearch" class="btn btn-success" runat="server" CommandName="search">Go</asp:LinkButton>
                                </div>
                                <div style="clear:both"> </div>
                            </FooterTemplate>
                        </asp:DataList>

2.后端代码

       protected void Page_Load(object sender, EventArgs e)
       {

            if (!IsPostBack)
            {
                    BindDataList(0);
             }

        private void BindDataList(int currentpage)
        {
            pds.AllowPaging = true;//允许分页
            pds.PageSize = 10;//每页显示的信息条数
            pds.CurrentPageIndex = currentpage;
            string strSql = "select *from tb_leaveword order by leaveTime desc";//mysql查询语句
            conn.Open();//打开数据库
            MySqlDataAdapter sda = new MySqlDataAdapter(strSql, conn);//执行mysql语句
            DataSet ds = new DataSet();
            sda.Fill(ds);//填充数据集
            pds.DataSource = ds.Tables[0].DefaultView;
            DataList1.DataSource = pds;//设置DataList的数据源
            DataList1.DataBind();//绑定数据
            conn.Close();//关闭数据库
        }
      
        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                Label CurrentPage = e.Item.FindControl("labCurrentPage") as Label;//获取DataList控件中的其他控件对象
                Label PageCount = e.Item.FindControl("labPageCount") as Label;
                Button FirstPage = e.Item.FindControl("lnkbtnFirst") as Button;
                Button PrePage = e.Item.FindControl("lnkbtnFront") as Button;
                Button NextPage = e.Item.FindControl("lnkbtnNext") as Button;
                Button LastPage = e.Item.FindControl("lnkbtnLast") as Button;
                CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
                PageCount.Text = pds.PageCount.ToString();
                if (pds.IsFirstPage)//如果当前页是首页
                {
                    FirstPage.Enabled = false;//首页按钮禁止
                    PrePage.Enabled = false;//上一页按钮禁止
                }
                if (pds.IsLastPage)
                {
                    NextPage.Enabled = false;
                    LastPage.Enabled = false;
                }
            }
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "first":
                    pds.CurrentPageIndex = 0;
                    BindDataList(pds.CurrentPageIndex);
                    break;
                case "pre":
                    pds.CurrentPageIndex = pds.CurrentPageIndex - 1;
                    BindDataList(pds.CurrentPageIndex);
                    break;
                case "next":
                    pds.CurrentPageIndex = pds.CurrentPageIndex + 1;
                    BindDataList(pds.CurrentPageIndex);
                    break;
                case "last":
                    pds.CurrentPageIndex = pds.PageCount - 1;
                    BindDataList(pds.CurrentPageIndex);
                    break;
                case "search":
                    if (e.Item.ItemType == ListItemType.Footer)
                    {
                        int PageCount = int.Parse(pds.PageCount.ToString());
                        TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;
                        int MyPageNum = 0;
                        if (!txtPage.Text.Equals(""))
                        {
                            MyPageNum = Convert.ToInt32(txtPage.Text.ToString());
                        }
                        if (MyPageNum <= 0 || MyPageNum > PageCount)
                        {
                            Response.Write("<script>alert('请输入页数并确定没有超出总页数!')</script>");
                        }
                        else
                        {
                            BindDataList(MyPageNum - 1);
                        }
                    }
                    break;
            }
        }
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值