I am working on a multilingual application. For this reason, I created custom BasePage class, which contains overriden InitializeCulture method.

BasePage class inherits System.Web.UI.Page class and all Content Page classess inherits from BasePage. And everything is OK, but one problem exists:

ContentPages can't access it's own controls, when inheriting from BasePage (instead of System.Web.UI.Page).

When I try to reference any control in Content Page code - there is always Nullreference Exception thrown.

Any suggestions about dealing with this problem will be appreciated.

有帮助吗?

解决方案

I just found out that removing references to Button Master Page's Button Controls - fixed my problem!

其他提示

here's a BasePage class code:

namespace MultiLingualApp {

public class BasePage : System.Web.UI.Page
{

    protected override void InitializeCulture()
    {
        string selectedLanguage = "en-GB";
        HttpCookie langCookie = Request.Cookies["langCookie"];
        if (langCookie == null)
        {
            Button btnEnglish = (Button)this.Master.FindControl("btnEnglish");
            btnEnglish.Enabled = false;
        }
        else
        {
            if (!string.IsNullOrEmpty(langCookie.Values["lang"]))
            {

                string cookie = langCookie.Values["lang"].ToString();
                if (cookie.Equals("en-GB"))
                {
                    selectedLanguage = "en-GB";
                    Button btnEnglish = (Button)this.Master.FindControl("btnEnglish");
                    btnEnglish.Enabled = false;

                }
                else if (cookie.Equals("th-TH"))
                {
                    selectedLanguage = "th-TH";
                    Button btnThai = (Button)this.Master.FindControl("btnThai");
                    btnThai.Enabled = false;
               }
            }
        }


        if (selectedLanguage != null)
        {
            UICulture = selectedLanguage;
            Culture = selectedLanguage;
            Thread.CurrentThread.CurrentCulture =
            CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new
            CultureInfo(selectedLanguage);
        }

        base.InitializeCulture();
    }

}

}

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top