I have a master page and a child page (page using master page).
I have a class/function that I call from the child page (like this):
new MasterPageSiteWide().setGeoAndLanguage(ViewState["_country"].ToString(), _lang);
This calls the function (as when I step through it executes the values).
On the master page I have:
public partial class MasterPageSiteWide : System.Web.UI.MasterPage
{
public string _closeTimeType;
public string _langForCalendar;
public void setGeoAndLanguage(string nation, string lang)
{
string globalresourcestring = string.Empty;
_langForCalendar = (String)GetGlobalResourceObject("share", "lblCalendarLang");
switch (nation.ToUpper())
{
case "USA":
ViewState["geo"] = "USA";
ViewState["langID"] = 1;
_closeTimeType = "PTPA";
break;
case "CAN":
if (lang.ToUpper() == "FR")
{
ViewState["geo"] = "CAN";
ViewState["langID"] = 2;
_closeTimeType = "CANPA";
}
else
{
ViewState["geo"] = "CAN";
ViewState["langID"] = 1;
_closeTimeType = "CANPA";
}
break;
case "MEX":
ViewState["geo"] = "MEX";
ViewState["langID"] = 3;
_closeTimeType = "MEXPA";
break;
default:
ViewState["geo"] = "USA";
ViewState["langID"] = 1;
_closeTimeType = "PTPA";
break;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
When the code steps through it sets the variables _closeTimeType and _langForClaendar (I can see the values).
But when it goes back to the child page those values are null. I am accessing them like this:
Master._closeTimeType
Also the ViewState variables are null after the master page is loaded.
NOTE: I do have this line on the .aspx page:
<%@ MasterType virtualPath="~/MasterPageSiteWide.master"%>
Why are the variables not keeping the values I am setting?
Based on your example, I think you may be unintentionally instantiating an additional master page, when in reality you want to call the functions on the master page upon which the existing page instance is based. You might try something akin to the following cast from the child page:
You may also be able to just try it directly, eg
That should call the master page function from the current page instance, and cause the page to behave as you expect (preserving the values). Hope that helps.