【发布时间】: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 已满,谢谢但下一篇文章给了我答案