Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Property in master page prevents controls from being created 1

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
0
0
US
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.

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
 
Actually, if you access the property on the master page from "InitializeCulture", it won't create the controls.

You can access property from the Page_Load and Page_Init events without any affects.

Why does accessing the property cause the controls not to be created?

Thanks,

Tom
 
Tom,

I think you posted this in the wrong forum....that doesn't look anything like Microsoft SQL Server code. I suggest posting your query in the appropriate forum.

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top