2012-01-02 56 views
2

我無法在全球化的母版頁中使用以下代碼&本地化。它給出了錯誤的代碼部分評論說:「不包含認定中的InitializeCulture」ASP.NET網頁全球化和主頁中的本地化C#3.0

protected override void InitializeCulture() 
    { 
     if (Request["Language"] != null) 
     { 
      //String selectedLanguage = Request["Language"]; 
      // code wil go here 

     } 
     base.InitializeCulture(); 
     //base.InitializeCulture gives error as mentioned in the next line 
     //does not contain a defination for InitializeCulture 
    } 

當我將此代碼添加到比母版頁等其他網頁正常工作。在Master Page中使用此代碼是否有任何限制。

如果我能夠在母版頁中定義此代碼,那麼我不需要在每個文件中編寫此代碼。

難道我做錯了什麼,我有包括線程和全球化文件,不過它並沒有在母版頁

回答

3

工作,你必須在你的頁面類來做到這一點(=覆蓋InitializeCulture)。它在母版頁中不起作用(母版頁來自控制,而不是來自頁面)。我建議你實現一個從Page派生的基類,並從這個類中派生出每一個Web表單,然後你也只需要編寫一次代碼。擁有自己的基礎班總是很方便。

在Visual Studio中添加一個新的類PageBase.cs:

public class FormBase : Page 
{ 
    protected override InitializeCulture() 
    { 
     if (Request.Form["lbCulture"] != null) 
     { 
     String selectedLanguage = Request.Form["lbCulture"]; 
     UICulture = selectedLanguage; 
     Culture = selectedLanguage; 

     Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage); 
     Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage); 
     } 
     base.InitializeCulture(); 
    } 
} 

當前區域性要麼存放在某些下拉列表框,在會話或查詢字符串傳遞。我在示例中使用了一個列表框。

然後你從這個頁面獲得你的WebForm這樣的:

public class Default : FormBase // instead of deriving from Page 
+0

我不知道我怎麼做,我是新來的節目..我會嘗試。謝謝 – Learning 2012-01-02 16:00:53

+0

我添加了一些代碼。希望這可以幫助。如果它是正確的,請將我的答案標記爲已回答。 – slfan 2012-01-02 16:48:45

+0

@slfan如果我想本地化一個位於'MasterPage'上的標籤,所以你說不使用'MasterPage',而是讓你擁有從'Page'繼承的BaseMasterPage。 – PUG 2012-10-10 19:07:24