每次s_Sort不更新SortDirection.Desc
时如何做到这一点private SortDirection s_Sort = SortDirection.Desc;
protected void Page_Load(object sender, EventArgs e)
{
lblSort.Text = S_Sort.ToString();//every time == SortDirection.Desc - this is bad!
if (!IsPostBack)
{
ShowTree();
Validate();
}
}
需要
public void btnSortUp_Click(object sender, EventArgs e)
{
S_Sort = SortDirection.Asc;
}
public void btnSortDown_Click(object sender, EventArgs e)
{
S_Sort = SortDirection.Desc;
}
但在SortDirection.Desc错误之后
答案 0 :(得分:3)
这是ASP.NET生命周期的问题。每次发生回发时(例如,单击btnSortUp或btnSortDown时),都会创建页面的新实例,即S_Sort重新初始化为Desc。如果要在回发之间保留值,可以将其存储在视图状态中,例如,通过将其封装在私有属性中:
private SortDirection S_Sort {
get { return (SortDirection)(ViewState["S_Sort"] ?? SortDirection.Desc); }
set { ViewState["S_Sort"] = value; }
}