在ASP.Net Gridview控件中实现分页

时间:2014-09-05 09:33:42

标签: c# asp.net gridview footer

我有一个绑定在页面加载上的gridview,并且我使用页脚行技术实现了插入。

现在我在同一个gridview中尝试了分页。

我没有错误,但在页脚行的DDL中更改页面时,我发现所有值都重复。

在第一页中,DDL中的输出为:

MSG
PAY
BUY
LIS

如果更改了页面,则第二页的第二个输出是:

MSG
PAY
BUY
LIS
MSG
PAY
BUY
LIS

我非常感谢任何建议。

谢谢!

我在RowDataBound中使用了这些行:

   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
        {
            DropDownList Area_DDL = (DropDownList)e.Row.FindControl("Area_DDL");
            Area_DDL.DataTextField = "area_name";
            Area_DDL.DataValueField = "area";
            Area_DDL.DataSource = Area();
            Area_DDL.DataBind();
            Area_DDL.Items.FindByValue((e.Row.FindControl("Area") as Label).Text).Selected = true;
        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            DropDownList Area_DDL = (DropDownList)e.Row.FindControl("Area_DDL");
            Area_DDL.DataTextField = "area_name";
            Area_DDL.DataValueField = "area";
            Area_DDL.DataSource = Area();
            Area_DDL.DataBind();
        }

        if (e.Row.RowType == DataControlRowType.Pager)
        {
            DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
            Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");

            if (lblPageCount != null)
                lblPageCount.Text = GridView1.PageCount.ToString();

            for (int i = 1; i <= GridView1.PageCount; i++)
            {
                ddl.Items.Add(i.ToString());
            }

            ddl.SelectedIndex = GridView1.PageIndex;

            if (GridView1.PageIndex == 0)
            {
                ((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
                ((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
            }

            if (GridView1.PageIndex + 1 == GridView1.PageCount)
            {
                ((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
                ((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

像这样更改你的代码,我希望它有所帮助:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridViewBind();
}

protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow gvrPager = GridView1.BottomPagerRow;
    DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");

    GridView1.PageIndex = ddlPages.SelectedIndex;
    GridViewBind();
}

protected void Paginate(object sender, CommandEventArgs e)
{
    int intCurIndex = GridView1.PageIndex;        

    switch (e.CommandArgument.ToString().ToLower())
    {
        case "First":
            GridView1.PageIndex = 0;                
            break;
        case "Prev":
            GridView1.PageIndex = intCurIndex - 1;
            break;
        case "Next":
            GridView1.PageIndex = intCurIndex + 1;
            break;
        case "Last":
            GridView1.PageIndex = GridView1.PageCount - 1;
            break;
    }
    GridView1.DataBind();
}


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Pager)
    {
        DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
        Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");

        if (lblPageCount != null)
        {
            lblPageCount.Text = GridView1.PageCount.ToString();

            for (int i = 1; i <= GridView1.PageCount; i++)
            {
                ddl.Items.Add(i.ToString());
            }

            ddl.SelectedIndex = GridView1.PageIndex;

            if (GridView1.PageIndex == 0)
            {
                ((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
                ((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
            }

            if (GridView1.PageIndex + 1 == GridView1.PageCount)
            {
                ((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
                ((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
            }
        }
    }
}