Datalist翻页(不用第三方控件,自己写翻页代码)

1控件准备:2个Label 一个用来绑定第几页,一个用来绑定总页数;5个linkbutton 分别为(首页、上一页、下一页、尾页、Go);1个textbox用来绑定跳转到的页数
2代码:首先在Page_load事件里面声明刚开始的页数为1,(注意:一定要在非回传页面下) 即 
         //非回传,非常重要  
         if (!IsPostBack)
            {
                Lcount.Text = "1";
                MoreBind();
            }
      // 绑定数据:
   public void CheckBind()
    {
        Infos i = new Infos();
        string tile = Ttile.Text;
        string content = Tcontent.Text;
        string puser = Tpuser.Text;
        //第几页
        int CurPage= Convert.ToInt32(Lcount.Text);
        //当前页面从Page查询参数获取           
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = i.QT_Select(tile, content, puser).DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 10;
        ps.CurrentPageIndex = CurPage - 1;
        //绑定总页数
        Lsl.Text = ps.PageCount.ToString();
        if (ps.IsFirstPage)
        {
            Lfrist.Enabled = false;
            Lpev.Enabled = false;
        }
        else
        {
            Lfrist.Enabled = true;
            Lpev.Enabled = true;
        }
        if (ps.IsLastPage)
        {
            Lnext.Enabled = false;
            Llast.Enabled = false;
        }
        else
        {
            Lnext.Enabled = true;
            Llast.Enabled = true;
        }
        DlMore.DataSource = ps;
        DlMore.DataBind();
    }
3 5个linkbutton 的单击事件:
    //首页
    protected void Lfrist_Click(object sender, EventArgs e)
    {
        Lcount.Text = "1";
        CheckBind();

    }
    //上一页
    protected void Lpev_Click(object sender, EventArgs e)
    {
        Lcount.Text = Convert.ToString(Convert.ToInt32(Lcount.Text) - 1);
        CheckBind();
    }
    //下一页
    protected void Lnext_Click(object sender, EventArgs e)
    {
        Lcount.Text = Convert.ToString(Convert.ToInt32(Lcount.Text) + 1);
        CheckBind();
    }
    //尾页
    protected void Llast_Click(object sender, EventArgs e)
    {
        Lcount.Text = Lsl.Text;
        CheckBind();
    }
    //跳转
    protected void LGo_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(TGO.Text) >= 1 && Convert.ToInt32(TGO.Text) <= Convert.ToInt32(Lsl.Text))
        {
            Lcount.Text = TGO.Text;
        }
        else
        {
            Response.Write("<script>alert('请输入正确页数!');</script>");
        }
        CheckBind();
        TGO.Text = "";
    }


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值