0

我有一个启用了分页的gridview。我在 gridview 的寻呼机上也有一个下拉菜单,用户可以在其中选择他们想要检索的每页记录的数量。更改下拉列表后,将触发一个事件(如下所示)以使用每个页面请求的更新结果重新运行查询。这很好用。但是,我确实希望在下拉列表中也有一个值“All”,而我用来暗示这一点的方法是禁用分页。

除了一个问题外,这一切都非常有效。当用户在下拉列表中选择“全部”时,我希望在更新 gridview 后仍显示寻呼机。它没有显示,因为我关闭了寻呼,但是有没有办法再次显示寻呼机?请参阅下面的事件代码。(如您所见,我在最后重新启用了寻呼机,但这没有效果)

谢谢达摩

下拉更改事件背后的代码

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

            }
        }
4

1 回答 1

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;
}
于 2012-09-19T21:54:48.483 回答