2012-09-19 53 views
0

我有一個已啓用分頁的GridView。我還在gridview的頁面上有一個下拉菜單,用戶可以選擇他們想要檢索的頁面數量。一旦更改下拉列表,將觸發一個事件(如下所示),以重新運行帶有每頁請求更新結果的查詢。這工作得很好。然而,我確實希望在下拉列表中有一個「All」值,而我用來實現這一點的方法是禁用分頁。Gridview分頁顯示所有記錄

這一切都非常出色,除了一個問題。當用戶在下拉列表中選擇「全部」時,我希望GridView更新後仍能顯示尋呼機。它沒有顯示,因爲我關閉了分頁,但有沒有辦法再次顯示分頁?查看我的代碼以獲取該事件。 (正如你可以看到我renable尋呼機在結束但這並沒有影響)

感謝 DAMO

背後事件

代碼下拉變化

void GridViewMainddl_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      //changes page size 
      if ((((DropDownList)sender).SelectedValue).ToString() == "All") 
      { 

       GridViewMain.AllowPaging = false; 

      } 
      else 
      { 
       GridViewMain.PageSize = int.Parse(((DropDownList)sender).SelectedValue); 

      } 



      //binds data source 
      Result fAuditOverallStatusLatest = new Result(sConn); 
      GridViewMain.DataSource = Result.getAuditOverallStatusLatest();    
      GridViewMain.PageIndex = 0; 
      GridViewMain.DataBind(); 
      GridViewMain.AllowPaging = true; 
      GridViewMain.BottomPagerRow.Visible = true; 
      GridViewMain.TopPagerRow.Visible = true; 
     } 

DDL代碼背後

protected void GridViewMain_RowCreated(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.Pager) 
      { 
       DropDownList GridViewMainddl = new DropDownList(); 
       //adds variants of pager size 
       GridViewMainddl.Items.Add("5"); 
       GridViewMainddl.Items.Add("10"); 
       GridViewMainddl.Items.Add("20"); 
       GridViewMainddl.Items.Add("50"); 
       GridViewMainddl.Items.Add("100"); 
       GridViewMainddl.Items.Add("200"); 
       GridViewMainddl.Items.Add("500"); 
       GridViewMainddl.Items.Add("All"); 
       GridViewMainddl.AutoPostBack = true; 
       //selects item due to the GridView current page size 
       ListItem li = GridViewMainddl.Items.FindByText(GridViewMain.PageSize.ToString()); 
       if (li != null) 
        GridViewMainddl.SelectedIndex = GridViewMainddl.Items.IndexOf(li); 
       GridViewMainddl.SelectedIndexChanged += new EventHandler(GridViewMainddl_SelectedIndexChanged); 
       //adds dropdownlist in the additional cell to the pager table 
       Table pagerTable = e.Row.Cells[0].Controls[0] as Table; 
       TableCell cell = new TableCell(); 
       cell.Style["padding-left"] = "15px"; 
       cell.Controls.Add(new LiteralControl("Page Size:")); 
       cell.Controls.Add(GridViewMainddl); 
       pagerTable.Rows[0].Cells.Add(cell); 
       //add current Page of total page count 
       TableCell cellPageNumber = new TableCell(); 
       cellPageNumber.Style["padding-left"] = "15px"; 
       cellPageNumber.Controls.Add(new LiteralControl("Page " + (GridViewMain.PageIndex + 1) + " of " + GridViewMain.PageCount)); 
       pagerTable.Rows[0].Cells.Add(cellPageNumber); 

      } 
     } 

回答

1

將其放入您的Page_Init

GridViewMain.PreRender += new EventHandler(GridViewMain_PreRender);

然後在您的Page類別處:

void GridViewMain_PreRender(object sender, EventArgs e) 
{ 
    var pagerRow = (sender as GridView).BottomPagerRow; 
    if (pagerRow != null) 
    { 
     pagerRow.Visible = true; 
    } 
} 

然後你下拉事件:

void GridViewMainddl_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    MyServices fServices = new FAServices(sConn); 
    Result fAuditOverallStatusLatest = new Result(sConn); 
    var data = Result.getAuditOverallStatusLatest(); 

    //changes page size 
    if ((((DropDownList)sender).SelectedValue).ToString() == "All") 
    { 
     GridViewMain.PageSize = data.Count(); 
    } 
    else 
    { 
     GridViewMain.PageSize = int.Parse(((DropDownList)sender).SelectedValue); 
    } 

    //binds data source 
    GridViewMain.DataSource = data;    
    GridViewMain.PageIndex = 0; 
    GridViewMain.DataBind(); 
    GridViewMain.AllowPaging = true; 
} 

在這種PreRender事件,你必須複製該頂級傳呼機的代碼。

編輯:若要選擇All,使GridViewMain_RowCreated這種變化:

if (li != null) 
{ 
    GridViewMainddl.SelectedIndex = GridViewMainddl.Items.IndexOf(li); 
} 
else 
{ 
    GridViewMainddl.SelectedIndex = GridViewMainddl.Items.Count - 1; 
} 
+0

喜,工作,但值「全部」未在DDL顯示? – user1438082

+0

嗨,你可以在你的答案_displayPager替換爲True;和data.Count;與data.Count(); 。它們是迄今爲止對您的代碼進行的唯一修改。你的代碼工作得很完美(用我上面的輕微的mod),但是當頁面重新繪製時,ddl中沒有顯示值「All」。它總是回覆到下拉的第一個值,即在我的情況下是5? (如果這有幫助,我將ddl代碼添加到上面的問題中)感謝damo – user1438082

+0

進行了這些編輯。另外,只需添加另一行以在創建菜單的代碼中將「All」添加到您的下拉列表中。 – Gromer