2012-01-02 96 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