2014-09-05 122 views
0

我有一個在頁面加載時綁定的gridview,並且我已經使用頁腳行技術實現了插入。在ASP.Net 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; 
      } 
     } 
    } 

回答

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

大,非常感謝你! – 2014-09-05 15:07:46