【问题标题】:Problems with Session for Sorting my GridView对我的 GridView 进行排序的会话问题
【发布时间】:2014-04-14 13:44:14
【问题描述】:

Session["TaskTable"] 作为我的 GridView 的数据源时遇到问题。 当我第一次打开 .aspx 站点时,Session["TaskTable"] 为空,如果我重新加载页面 (F5),Session["TaskTable"] 就是我的数据表 taskTable。怎么可能?如果我第一次重新加载我的页面,我只能排序。有任何想法吗?谢谢

protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            DataTable taskTable = new DataTable("TaskList");
            taskTable = dtCloned;                
            Session["TaskTable"] = taskTable;
            GV_Projekte.DataSource = Session["TaskTable"];
            GV_Projekte.DataBind();

         }
     }

对于我的 GridView 的排序

protected void gv_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dt = Session["TaskTable"] as DataTable;

        if (dt != null)
        {
            //Sort the data.
            dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
            GV_Projekte.DataSource = Session["TaskTable"];
            GV_Projekte.DataBind();
        }
    }

    private string GetSortDirection(string column)
    {
        // By default, set the sort direction to ascending.
        string sortDirection = "ASC";

        // Retrieve the last column that was sorted.
        string sortExpression = ViewState["SortExpression"] as string;

        if (sortExpression != null)
        {
            // Check if the same column is being sorted.
            // Otherwise, the default value can be returned.
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection = "DESC";
                }
            }
        }

        // Save new values in ViewState.
        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;

        return sortDirection;
    }

【问题讨论】:

  • dtCloned 来自哪里?跟踪代码行:taskTable = dtCloned;看看 dtCloned 是否不为空
  • dtCloned 已满,谢谢但下一篇文章给了我答案

标签: c# asp.net gridview


【解决方案1】:

这是因为 Page.IsPostback 条件当页面未回发时,它会将会话分配给一个变量,现在该值将一直存在,直到会话到期,无论会话超时。

进行以下练习。

 protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["a"]==null)
        {
            Response.Write("Session is empty");
        }
        else
        {
            Response.Write("Session is not empty");
            Response.Write(Session["a"].ToString());
        }
        if (!Page.IsPostBack)
        {
            Session["a"] = "Jalpesh";
        }
    }

在空白页面中,我正在做与您相同的事情,但第一次页面将在那时加载,它会打印“会话为空”,但在会话到期之前,该会话不会为空,因此它将打印“会话”不为空”。

因此,如果您需要会话值为 null,则必须在某处将会话分配给 null。

【讨论】:

  • 感谢您的信息,现在我可以看看如何更改会话的使用
【解决方案2】:

我找到了解决方案,函数 gv_Sorting 必须位于 page_load 之前,以便第一次正确填充。另请参阅我从中获取代码的示例: http://msdn.microsoft.com/de-de/library/system.web.ui.webcontrols.gridview.sorting(v=vs.110).aspx

【讨论】:

    猜你喜欢
    • 2020-07-08
    • 2012-08-09
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    相关资源
    最近更新 更多