在ASP.Net网格视图控件中实现分页

本文关键字:实现 分页 控件 视图 ASP Net 网格 | 更新日期: 2023-09-27 18:20:41

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

现在我在同一个网格视图中尝试了分页。

我没有错误,但当在页脚行的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;
            }
        }
    }

在ASP.Net网格视图控件中实现分页

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

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;
            }
        }
    }
}