分页

<asp:LinkButton ID="lnkFirst" runat="server" CausesValidation="False" CommandArgument="First"
                    OnClick="LinkButton_Click" Text="首页"></asp:LinkButton>
                <asp:LinkButton ID="lnkPre" runat="server" CausesValidation="False" CommandArgument="Pre"
                    OnClick="LinkButton_Click" Text="上一页"></asp:LinkButton>
                <asp:LinkButton ID="lnkNext" runat="server" CausesValidation="False" CommandArgument="Next"
                    OnClick="LinkButton_Click" Text="下一页"></asp:LinkButton>
                <asp:LinkButton ID="lnkLast" runat="server" CausesValidation="False" CommandArgument="Last"
                    OnClick="LinkButton_Click" Text="尾页"></asp:LinkButton>

 

//此方法写在数据绑定里面


 #region 分页前方法
        //分页前方法
        Pager(); 
        #endregion


#region 分页前的加载
    /// <summary>
    /// 分页前的加载
    /// </summary>
    protected void Pager()
    {
        //分页
        lnkPre.Enabled = true;
        lnkNext.Enabled = true;
        if (gvLoanList.Rows.Count > 0)
        {
            if (gvLoanList.PageIndex == 0)
                lnkPre.Enabled = false;
            if (gvLoanList.PageIndex == gvLoanList.PageCount - 1)
                lnkNext.Enabled = false;
            ddlPage.ClearSelection();
            ddlPage.Items.Clear();
            for (int j = 1; j <= gvLoanList.PageCount; j++)
            {
                ddlPage.Items.Add(new ListItem("第" + j.ToString() + "/" + gvLoanList.PageCount.ToString() + "页", j.ToString()));
                lblPage.Text = "当前第<strong>" + Convert.ToInt32(gvLoanList.PageIndex + 1).ToString() + "</strong>页,共<strong>" + Convert.ToInt32(gvLoanList.PageCount).ToString() + "</strong>页";
            }
            ddlPage.SelectedIndex = gvLoanList.PageIndex;
        }
        else
        {
            ddlPage.ClearSelection();
            ddlPage.Items.Clear();
            lnkPre.Enabled = false;
            lnkNext.Enabled = false;
            ddlPage.Items.Add("第1/1页");
            lblPage.Text = "当前第<strong>1</strong>页,共<strong>1</strong>页";
        }
    } 
    #endregion


#region 分页
    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LinkButton_Click(object sender, EventArgs e)
    {
        LinkButton lb = sender as LinkButton;
        if (lb.CommandArgument.ToString() == "Next")
            this.gvLoanList.PageIndex++;
        if (lb.CommandArgument.ToString() == "Pre")
            this.gvLoanList.PageIndex--;
        if (lb.CommandArgument.ToString() == "First")
            this.gvLoanList.PageIndex = 0;
        if (lb.CommandArgument.ToString() == "Last")
            this.gvLoanList.PageIndex = gvLoanList.PageCount;

        DataBind();
    } 
    #endregion



    #region 下拉框分页
    /// <summary>
    /// 下拉框分页
    /// </summary>
    protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
    {
	gvCompanyDirectorInfo.PageIndex = Convert.ToInt32(ddlPage.SelectedValue) - 1;
        BindData();
    }
    
    #endregion

 

posted @ 2014-06-17 10:47  团长丶Joe  阅读(102)  评论(0编辑  收藏  举报