应该如何正确使用会话变量来对 GridView 进行分页和排序?

发布于 2024-11-20 00:45:47 字数 2321 浏览 8 评论 0原文

我有一个 ASP .NET 页面,其中包含几个 GridView 控件,我正在为其实现排序和分页。

我使用会话变量来维护表示 ViewState 中 GridView 数据给定页面的 DataTable,如下所示:

protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
      // Session["Page"] represents the active page of the GridView 
      // when the Sorting event fires.
      DataTable dt = Session["Page"] as DataTable;

      if (dt != null)
      {       
           if (ViewState["SortDirection"] == null)
           {
                ViewState["SortDirection"] = "DESC";
           }

           string ViewState_SortDirection = ViewState["SortDirection"].ToString();

           for (int i = 0; i <= ((GridView)sender).Columns.Count - 1; i++)
           {
                if (e.SortExpression == ((GridView)sender).Columns[i].SortExpression)
                {
                     if (ViewState["SortDirection"].ToString() == "ASC")
                     {
                          e.SortDirection = SortDirection.Descending;
                          ((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText + " ▼";
                          ViewState["SortDirection"] = "DESC";
                     }
                     else if (ViewState["SortDirection"].ToString() == "DESC")
                     {
                          e.SortDirection = SortDirection.Ascending;
                          ((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText + " ▲";
                          ViewState["SortDirection"] = "ASC";
                     }
                }
           }

           DataView dv = new DataView(dt)
           {
                Sort = e.SortExpression + " " + ViewState["SortDirection"]
           };

           gv.DataSource = dv;
           gv.DataBind();

           Session["Page"] = dv.ToTable();
           DataTable dt = Session["Page"] as DataTable;
      }
 }

我希望每个 GridView 使用相同的 Sorting 事件处理程序。当状态包中的会话变量(如 Session["Page"])正在使用时,该会话变量是否特定于其排序事件触发的 GridView?或者可以由其他 GridView 控件使用它来修改它以在同一页面上进行排序吗?意思是,如果我有另一个也使用 Session["Page"] 进行分页的 GridView,那么该会话变量是否在该控件的范围内?

或者,我应该遵循这篇文章的答案并通过只有每个会话的 SortDirection?

I have an ASP .NET page with several GridView controls for which I'm implementing sorting and paging.

I'm using session variables to maintain a DataTable representing a given page of the GridView data in the ViewState like so:

protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
      // Session["Page"] represents the active page of the GridView 
      // when the Sorting event fires.
      DataTable dt = Session["Page"] as DataTable;

      if (dt != null)
      {       
           if (ViewState["SortDirection"] == null)
           {
                ViewState["SortDirection"] = "DESC";
           }

           string ViewState_SortDirection = ViewState["SortDirection"].ToString();

           for (int i = 0; i <= ((GridView)sender).Columns.Count - 1; i++)
           {
                if (e.SortExpression == ((GridView)sender).Columns[i].SortExpression)
                {
                     if (ViewState["SortDirection"].ToString() == "ASC")
                     {
                          e.SortDirection = SortDirection.Descending;
                          ((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText + " ▼";
                          ViewState["SortDirection"] = "DESC";
                     }
                     else if (ViewState["SortDirection"].ToString() == "DESC")
                     {
                          e.SortDirection = SortDirection.Ascending;
                          ((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText + " ▲";
                          ViewState["SortDirection"] = "ASC";
                     }
                }
           }

           DataView dv = new DataView(dt)
           {
                Sort = e.SortExpression + " " + ViewState["SortDirection"]
           };

           gv.DataSource = dv;
           gv.DataBind();

           Session["Page"] = dv.ToTable();
           DataTable dt = Session["Page"] as DataTable;
      }
 }

I'd like to have each GridView use the same Sorting event handler. When session variables in the state bag like Session["Page"] are in use, is this session variable specific to the GridView whose Sorting event fires? Or can it be modified by other GridView controls using it to sort on the same page? Meaning, if I have another GridView which also uses Session["Page"] for paging, will that session variable be in that control's scope?

Or, should I just follow the lead of this post's answer and pass only the SortDirection for each session?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2024-11-27 00:45:47

以下是可用的数据持久变量类型及其范围:

  • 应用程序变量(在整个应用程序的所有用户会话之间共享)
  • 会话变量(在整个用户会话的所有页面之间共享)
  • ViewState 变量(仅存在于页面上,对于整个页面)
  • ControlState 变量(仅存在于页面上,仅用于控件)

如果将网格数据存储为会话变量,则当您在同一页面中使用两个网格时,您将遇到问题。在不了解更多系统信息的情况下,我无法就如何存储数据给出任何具体建议,但是上面的变量类型可能会为您指明正确的方向

Here are the available data-persisting variable types and their scopes:

  • Application Variables (shared between all user sessions, for the entire application)
  • Session Variables (shared between all pages, for the entire user session)
  • ViewState Variables (exists solely on the page, for the entire page)
  • ControlState Variables (exists solely on the page, just for the control)

If you are storing your grid data as Session variables, you will run into issues when you use two grids in the same page. I cannot give any specific suggestions on how to store the data without knowing more about the system, but the types of variables above may point you in the right direction

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文