Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从InitializeCulture访问母版页中的属性会阻止创建控件_C#_Asp.net - Fatal编程技术网

C# 从InitializeCulture访问母版页中的属性会阻止创建控件

C# 从InitializeCulture访问母版页中的属性会阻止创建控件,c#,asp.net,C#,Asp.net,我一直在进行本地化工作,希望将语言置于Viewstate中,以便在用户控件中访问 我设置了一个测试项目来展示这个问题。在用户控件上,我只有一个标签 如果我没有访问母版页的viewstate(其中该行被注释掉),那么一切都会正常工作,标签也会被创建 如果我访问viewstate以从viewstate获取语言,我会得到一个object not found错误,该对象将为null。错误是: 对象引用未设置为对象的实例 变量“culture”确实从Viewstate获取字符串 如果我用母版页访问注释掉这

我一直在进行本地化工作,希望将语言置于Viewstate中,以便在用户控件中访问

我设置了一个测试项目来展示这个问题。在用户控件上,我只有一个标签

如果我没有访问母版页的viewstate(其中该行被注释掉),那么一切都会正常工作,标签也会被创建

如果我访问viewstate以从viewstate获取语言,我会得到一个object not found错误,该对象将为null。错误是:

对象引用未设置为对象的实例

变量“culture”确实从Viewstate获取字符串

如果我用母版页访问注释掉这一行,“文化”被设置好了,一切正常

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;
        }

    }
} 
母版页为:

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)
        {

        }
    }
} 
实际上,如果您从“InitializeCulture”访问母版页上的属性,它将不会创建控件

您可以从Page_Load和Page_Init事件访问属性,而不受任何影响

为什么访问属性会导致不创建控件

谢谢

汤姆

对象引用未设置为对象的实例

您可以将引用设置为null。返回“en-US”或String.null

if (ViewState["ocCulture"] == null) 
   ViewState["ocCulture"] = "en-US";
return (string)ViewState["ocCulture"];
试试这个:

if (ViewState["ocCulture"] == null)
        {
            ViewState["ocCulture"] = "en-US";
        }
        else 
       { 
          ViewState["ocCulture"] = "";
       }
return (string)ViewState["ocCulture"];

因为代码正在崩溃?当仅访问InitializeCulture重写方法(例如MasterPage mp=Master;)中的Page.Master属性时,问题已经出现。