其他分享
首页 > 其他分享> > User control's property loses value after a postback

User control's property loses value after a postback

作者:互联网

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.)

 

 

 

 

标签:control,MSDN,after,int,postback,value,departmentID,ViewState
来源: https://www.cnblogs.com/chucklu/p/10833645.html