Last post Oct 02, 2014 02:44 AM by Jamobor yao - MSFT
Sep 30, 2014 11:36 AM|pelalusa|LINK
With most of the User Controls I've built, I just utilize the default constructor, which has no parameters. However, with one new User Control (UC), I need to pass a parameter to it. The parameter is a boolean value. Here's a simplified version of the
constructor and how I use the incoming value:
public MyUserControl(bool isSpecialMode)
IsSpecialMode = isSpecialMode;
private bool IsSpecialMode
ViewState["IsSpecialMode"] = value;
The ViewState property is definitely being set correctly BUT when the Page_Load event occurs, the ViewState property has returned to
Is this simply a matter of incorrectly using ViewState in the Page lifecycle?
Would you change to storing the value in a Session variable or is there another way?
Oct 02, 2014 02:44 AM|Jamobor yao - MSFT|LINK
Please have a look at this thread:
http://stackoverflow.com/questions/8117490/pass-values-from-page-to-user-control , the ViewState property is null when the page load, but we can add a condition to exclude this exception, see the sample code from the link I provide, below is a snippet.
public string FirstName
if (ViewState["FirstName"] == null)
ViewState["FirstName"] = value;