2012-09-19 189 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