0

我有一个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 个其他功能,例如排序、过滤 gridview 的下拉菜单和一个也过滤 girdview 的按钮搜索。在其中任何 3 种模式中进行内联编辑时,我无法控制 gridview 所处的状态。在我的 gridview 标记中,我将 EnableViewState 和 ViewStateMode 都设置为 true。

如何在这些模式下保持网格视图的状态?


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;

}
4

1 回答 1

1

您不需要将整个表存储在SessionorViewState中。只需存储 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();
}

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

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

您可以在此处下载测试项目。

于 2013-12-08T03:41:49.417 回答