I have been working on localization and want to put the language into the Viewstate to access in a user control.
I have a test project I set up to show the problem. On the user control, I have only one label.
If I don't access the Master pages viewstate (where that line is commented out), all works well and the label is created.
If I access the viewstate to get the language from the viewstate, I get an object not found error and the object will be null. The error is:
Object reference not set to an instance of an object.
The variable "culture" does get the string from the Viewstate.
If I comment out the line with the master page access, "culture" is set and everything works fine.
And the Master page is:
Any idea why accessing the Viewstate would prevent the controls from being created?
Thanks,
Tom
I have a test project I set up to show the problem. On the user control, I have only one label.
If I don't access the Master pages viewstate (where that line is commented out), all works well and the label is created.
If I access the viewstate to get the language from the viewstate, I get an object not found error and the object will be null. The error is:
Object reference not set to an instance of an object.
The variable "culture" does get the string from the Viewstate.
If I comment out the line with the master page access, "culture" is set and everything works fine.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
namespace TestCulture
{
public partial class Sample : System.Web.UI.Page
{
protected override void InitializeCulture()
{
string culture;
culture = "en-US";
culture = "es-MX";
culture = ((TestCulture.Site1)Page.Master).zCulture;
base.InitializeCulture();
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_PreRender(object sender, EventArgs e)
{
lblTestLabel.Text = ((TestCulture.Site1)Page.Master).Name;
}
}
}
And the Master page is:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestCulture
{
public partial class Site1 : System.Web.UI.MasterPage
{
public string zCulture
{
get
{
if (ViewState["ocCulture"] == null)
ViewState["ocCulture"] = "en-US";
return (string)ViewState["ocCulture"];
}
set { ViewState["ocCulture"] = value; }
}
public string Name
{
get
{
if (ViewState["Name"] == null)
ViewState["Name"] = "Tom";
return (string)ViewState["Name"];
}
set { ViewState["ocCulture"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
Any idea why accessing the Viewstate would prevent the controls from being created?
Thanks,
Tom