转到新页面时,C#排序丢失

时间:2016-04-13 09:33:52

标签: c# asp.net sorting gridview

在页面之间导航时,我的排序存在问题。当我转到新页面时,排序顺序丢失,用户必须再次排序。

我已经浏览了我的代码并且知道问题出在gridview的PageIndexChanging事件中。在哪里用新数据重新绑定gridview。

但是,我不确定如何避免这种情况?重新绑定gridview时如何存储排序顺序?也许是ViewState?

有什么建议吗?

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    PopulateProductClass();
    PopulateProduct();
    PopulateOrderList();  
  }          
}

private void PopulateOrderList()
{
  DateTime d;
  DateTime d2;

  CustomerInfo ki = CustomerInfoProvider.GetCustomerInfoByUserID(CooneenHelper.GetUserImpersonisationID());
  int nKustomerID = ki.CustomerID;
  DataTable dts = new DataTable();
  dts.Columns.Add("OrderDate", typeof(DateTime));
  dts.Columns.Add("OrderNumber", typeof(string));
  dts.Columns.Add("OrderItemSKUName", typeof(string));
  dts.Columns.Add("SKUNumber", typeof(string));
  dts.Columns.Add("OrderItemStatus", typeof(string));
  dts.Columns.Add("OrderItemUnitCount", typeof(string));
  dts.Columns.Add("mtrx_Code2", typeof(string));

  QueryDataParameters qdp = new QueryDataParameters();
  qdp.Add("@CustomerID", nKustomerID);

  if (drpProductClass.SelectedValue.ToString() != "0" || drpProductClass.SelectedValue.ToString() == null) { qdp.Add("@OrderItemWRClass", drpProductClass.SelectedItem.ToString()); }
  if (drpProduct.SelectedValue.ToString() != "0") { qdp.Add("@OrderItemSKUID", drpProduct.SelectedValue.ToString()); }                
  if (txtStartDate.Text != "") { d = DateTime.Parse(txtStartDate.Text); qdp.Add("@OrderItemDateFrom", d.ToString("yyyy-MM-dd")); }
  if (txtEndDate.Text != "") { d2 = DateTime.Parse(txtEndDate.Text); qdp.Add("@OrderItemDateTo", d2.ToString("yyyy-MM-dd")); }

  DataSet ds = gc.ExecuteQuery("CN_GetOrderItemByCustID", qdp, QueryTypeEnum.StoredProcedure, true);
  foreach (DataRow dr in ds.Tables[0].Rows)
  {
    DataRow drNew = dts.NewRow();
    drNew["OrderDate"] = ValidationHelper.GetDateTime(dr["OrderDate"], DateTime.Now).ToShortDateString();
    drNew["OrderNumber"] = dr["OrderNumber"].ToString();
    drNew["OrderItemSKUName"] = dr["OrderItemSKUName"].ToString();
    drNew["SKUNumber"] = dr["SKUNumber"].ToString();
    drNew["OrderItemStatus"] = dr["OrderItemStatus"].ToString();
    drNew["OrderItemUnitCount"] = dr["OrderItemUnitCount"].ToString();
    drNew["mtrx_Code2"] = dr["mtrx_Code2"].ToString();
    dts.Rows.Add(drNew);
  }
  //Clear the TextBox
  litResults.Text = String.Empty;
  if (dts.Rows.Count == 1)
    litResults.Text = "" + dts.Rows.Count.ToString() + " Order Items";
  else
    litResults.Text = "" + dts.Rows.Count.ToString() + " Order Items";      
  try
  {
    Session["Data"] = dts;
  }
  catch(Exception ex)
  {
    throw ex;
  }
  gvOrderItems.Visible = true;
  gvOrderItems.DataSource = dts.DefaultView;      
  gvOrderItems.DataBind();      
  if (dts.Rows.Count > 1) litResults.Text += " - Showing page " + (gvOrderItems.PageIndex + 1).ToString() + " of " + gvOrderItems.PageCount.ToString();
}

private string SortCriteria
{
  get
  {
    if (ViewState["sortCriteria"] == null)
    {
      ViewState["sortCriteria"] = "";
    }

    return ViewState["sortCriteria"].ToString();
  }
  set
  {
    ViewState["sortCriteria"] = value;
  }
}

public SortDirection SortDirection
{
  get
  {
    if (ViewState["SortDirection"] == null)
    {
      ViewState["SortDirection"] = SortDirection.Ascending;          
    }
    return (SortDirection)ViewState["SortDirection"];
  }
  set
  {
    ViewState["SortDirection"] = value;
  } 
}

protected void gvOrderItems_Sorting(object sender, GridViewSortEventArgs e)
{
  string sortExpression = e.SortExpression;
  string direction = string.Empty;

  DataTable dt = (DataTable)Session["Data"];

  if (dt.Rows.Count > 0)
  {        
    DataView dataView = new DataView(dt);

    if (SortDirection == SortDirection.Ascending)
    {
      SortDirection = SortDirection.Descending;         
      direction = " DESC";
    }
    else
    {
      SortDirection = SortDirection.Ascending;
      direction = " ASC";
    }
    dataView.Sort = sortExpression + direction;
    gvOrderItems.DataSource = dataView;
    gvOrderItems.DataBind();        
  }
} 

protected void gvOrderItems_PageIndexChanging1(object sender, GridViewPageEventArgs e)
{
  gvOrderItems.PageIndex = e.NewPageIndex;
  PopulateOrderList();
}

2 个答案:

答案 0 :(得分:0)

也许您在Page_Load的每个回发中都对GridView进行了数据绑定。

你应该只在第一次这样做。使用Page.IsPostBack属性:

protected void Page_Load(object sender, System.EventArgs e)
{
   if(!Page.IsPostBack)
       // GridBind here
}

答案 1 :(得分:0)

感谢Vignesh建议使用ViewState解决了我的问题。但是我选择使用Session变量。

在排序事件处理程序中,在执行排序后,我将排序列表存储在会话Session["SortedView"] = dataView;中。现在在PageIndexChanging1期间,我检查此会话变量是否为空并相应地使用。

protected void gvOrderItems_PageIndexChanging1(object sender, GridViewPageEventArgs e)
{
  gvOrderItems.PageIndex = e.NewPageIndex;
  if (Session["SortedView"] != null)
  {
    gvOrderItems.DataSource = Session["SortedView"];
    gvOrderItems.DataBind();
  }
  else
  {
    PopulateOrderList();
  }      
}