I'm developing an ASP.NET Webforms application and while using the WebUserControl I stumbled upon a behavior I dont understand.
My WebUserControl puts 4 strings into the ViewState:
var projectCode = SelectedProject.Value;
ViewState["ProjectCode"] = projectCode;
var resourceId = SelectedResource.Value;
ViewState["ResourceCode"] = resourceId;
var indicatorId = SelectedIndicator.Value;
ViewState["IndicatorCode"] = indicatorId;
var areaCode = SelectedArea.Value;
ViewState["AreaCode"] = areaCode;
When thats done, my page should get these values from the ViewState, but the ViewState does not contain any items and returns null
for all 4 keys.
Why is the ViewState on the page level empty? And if I shouldn't use the ViewState for this, whats the best method to pass values forth and back?
Because the keys you are using are local for the current usercontrol: the "AreaCode" defined in the viewstate for the UserControl is not the same as the one defined for the page. This behaviour is different from the Session one.
You can use the Session dictionary or implement some public methods on your user control to retrieve the values.