User control's property loses value after a postback
All variables (and controls) are disposed at the end of the page's lifecycle. So you need a way to persist your variable, e.g. in the ViewState
.
public int DepartmentID
{
get {
if (ViewState["departmentID"] == null)
return int.MinValue;
else
return (int)ViewState["departmentID"];
}
set { ViewState["departmentID"] = value; }
}
The MSDN Magazine article "Nine Options for Managing Persistent User State in Your ASP.NET Application" is useful, but it's no longer viewable on the MSDN website. It's in the April 2003 edition, which you can download as a Windows Help file (.chm). (Don't forget to bring up the properties and unblock the "this was downloaded from the internet" thing, otherwise you can't view the articles.)
根据How to choose from Viewstate, SessionState, Cookies and Cache,对于配置文件中的数据,还是直接写在cache或者application级别比较好。不用考虑数据丢失的问题