保持Gridview的状态

时间:2013-11-09 21:56:22

标签: c# asp.net gridview viewstate

我有一个gridview,我正试图保持状态。目前我有用户可以在线编辑的内容(在gridview中)。我经常这样做:

protected void GridViewTower_RowEditing(object sender, GridViewEditEventArgs e)
{
    //Set the edit index.
    GridViewTower.EditIndex = e.NewEditIndex;

    //Bind/Re-LoadData data to the GridView control.
    LoadData();
    Populate();
}

protected void GridViewTower_CancelEditRow(object sender, GridViewCancelEditEventArgs e)
{
    //Reset the edit index.
    GridViewTower.EditIndex = -1;

    //Bind/Re-LoadData data to the GridView control.
    LoadData();
    Populate();
}

问题是,我有3个其他功能,如排序,过滤网格视图的下拉菜单,以及还过滤网格视图的按钮搜索。在任何3种模式中进行内联编辑时,我无法控制gridview所处的状态。在gridview标记内,我将EnableViewState和ViewStateMode都设置为true。

如何在这些模式下保持gridview的状态?


public void LoadData()
{

    if (Session["GridView"] != null)
    {

        GridViewTower.DataSource = Session["GridView"];

        GridViewTower.DataBind();

        //Response.Redirect("TowerManagement.aspx"); //

        //Session["GridView"] = null;
    }
    else
    {

        WISSModel.WISSEntities context = new WISSModel.WISSEntities();

        var tower = (from t in context.Towers
                     where t.isDeleted == false
                     select new
                     {
                         t.TowerId,
                         t.TowerName,
                         RangeName = t.Range.RangeName

                     }).ToList();

        GridViewTower.DataSource = tower;
        GridViewTower.DataBind();

        ViewState["Sort"] = 0;
    }
}

protected void Gridview_Sort(object sender, GridViewSortEventArgs e)
{


    WISSModel.WISSEntities context = new WISSModel.WISSEntities();

    var towers = (from t in context.Towers
                  where t.isDeleted == false
                  select new
                  {
                      t.TowerId,
                      t.TowerName,
                      rangeName = t.Range.RangeName
                  }).ToList();

    DataTable gridviewTable = towers.CopyToDataTable();

    gridviewTable.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

    GridViewTower.DataSource = gridviewTable;

    GridViewTower.DataBind();

    Session["GridView"] = GridViewTower.DataSource;

}

1 个答案:

答案 0 :(得分:0)

您无需在SessionViewState中存储整个表格。只需存储SortExpression,SortOrder等的值。下面是一个如何操作的示例。

在我的代码中,我添加了两个私有属性来存储sortorder和sortexpression:

private string SortOrder
{
    get
    {
        // Toggle order after sorting
        string _order = "ASC";//Default
        if( ViewState["SortOrder"] != null && ViewState["SortOrder"].ToString() =="DESC")
        {
            _order = "DESC";
            ViewState["SortOrder"] = "ASC";
        }
        else
        {
            ViewState["SortOrder"] = "DESC";
        }
        return _order;
    }
    set
    {
        string _order = value.ToLower() == "descending"? "DESC" : "ASC";
        ViewState["SortOrder"] = _order;
    }
}


private string SortExpression
{
    get
    {
        return ViewState["SortExpression"] != null ? ViewState["SortExpression"].ToString() : "";
    }
    set
    {
        ViewState["SortExpression"] = value;
    }
}

我已经更改了GridView_Sort方法,以便在新添加的属性中存储排序表达式和排序顺序,并调用LoadData()方法:

protected void Gridview_Sort(object sender, GridViewSortEventArgs e)
{
    SortExpression = e.SortExpression;
    //Disabled sort direction to enable toggling
    //SortOrder = e.SortDirection.ToString();

    LoadData();
}

只要我们想要将数据加载到GridView中,就会从很多地方调用LoadData()方法。所以我把它改成了这个:

public void LoadData()
{
    WISSModel.WISSEntities context = new WISSModel.WISSEntities();

    var towers = (from t in context.Towers
                  where t.isDeleted == false
                  select new
                  {
                      t.TowerId,
                      t.TowerName,
                      rangeName = t.Range.RangeName
                  }).ToList();



    DataTable gridviewTable = new DataTable();
    gridviewTable.Columns.Add("TowerId");
    gridviewTable.Columns.Add("TowerName");
    gridviewTable.Columns.Add("rangeName");

    foreach (var t in towers)
    {
        gridviewTable.Rows.Add(new object[] { t.TowerId, t.TowerName, t.rangeName });
    }

    if (!String.IsNullOrEmpty(SortExpression))
    {
        gridviewTable.DefaultView.Sort = String.Format("{0} {1}", SortExpression, SortOrder);
        gridviewTable = gridviewTable.DefaultView.ToTable();
    }

    GridViewTower.DataSource = gridviewTable;
    GridViewTower.DataBind();
}

最初我在Page_Load()中调用LoadData()方法:

 protected void Page_Load(object sender, EventArgs e)
 {
      if (!IsPostBack)
      {
           LoadData();
      }
 }

您可以下载测试项目here