2012-06-26 29 views
1

当谈到Web应用程序时,我是一个小菜鸟。但我正在尽我所能,使用ASP.NET 2.0学习它,并对长篇文章感到抱歉。将母版页中的资源文化变化传播到内容网页

我有一个主页面(M1)和3个不同的内容页面C1,C2,C3,它们基本上使用主页面M1来填充内容占位符中的相应内容。

所有的Web表单都是本地化的,并且在资源(xml)文件中添加适当的语言资源字符串例如:Resource.en-US.xml,Resource.de-DE.xml等。最后,资源在建立适当的当前文化和当前的养蚕之后,在代码中提及。

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); 
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); 
//where the btnSubmit is a control on the form 
btnSubmit.Text = rm.GetString("Submit", Thread.CurrentThread.CurrentCulture); 

现在问题来了,我已经包括更改显示语言在母版页提供给用户作为一个asp的选择:与ASP的LinkBut​​ton:图像。每当用户单击所需语言的链接按钮时,内容页面控件应显示与所选文化相对应的全部内容字符串。

  1. 我该如何做到这一点?

  2. 我是否必须实施会话变量以包含所选的 语言?或者存储在Cookie中也可以完成这项工作?

我试图

在母版页加载事件。我尝试调用方法SetCultureSpecificInformation,它基本上设置CurrentThread的文化和uiculture属性并将选定的语言存储在会话变量中。

在asp:linkbutton OnClick事件处理程序上也有类似的实现。在这种情况下,它会修改会话变量。

最后在内容网页的OnPage_Load事件中引用session变量。

但不知何故,上述方法不会产生预期的结果。语言的切换不一致。任何人都可以用一种足够好的设计方法帮助我实现它。

在此先感谢

回答

2

添加全球。ASAX文件:写这篇文章的代码

void Application_BeginRequest(Object sender, EventArgs e) 
    { 
     // Code that runs on application startup 
     HttpCookie cookie = HttpContext.Current.Request.Cookies["CultureInfo"]; 
     if (cookie != null && cookie.Value != null) 
     { 
      System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie.Value); 
      System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value); 
     } 
     else 
     { 
      System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en"); 
      System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en"); 
     } 
    } 

而且

protected void ddlanguage_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     Session["language"] = ddlanguage.SelectedValue; 

     //Sets the cookie that is to be used by Global.asax 
     HttpCookie cookie = new HttpCookie("CultureInfo"); 
     cookie.Value = ddlanguage.SelectedValue; 
     Response.Cookies.Add(cookie); 

     //Set the culture and reload for immediate effect. 
     //Future effects are handled by Global.asax 
     Thread.CurrentThread.CurrentCulture = new CultureInfo(ddlanguage.SelectedValue); 
     Thread.CurrentThread.CurrentUICulture = new CultureInfo(ddlanguage.SelectedValue); 

     if (cookie.Value == "en") 
     { 
      Session["ddindex"] = 0; 
     } 

     else if (cookie.Value == "fr") 
     { 
      Session["ddindex"] = 1; 
     } 

     else if (cookie.Value == "de") 
     { 
      Session["ddindex"] = 2; 
     } 
     Server.Transfer(Request.Path); 
    } 
} 

Blog article: create multiple language website in asp.net c#

+1

目前我有类似的实现,但在母版页,如果我用** ** Server.Transfer的唯一的母版页更改区域性信息而不是内容页面。但奇怪的是** Response.Redirect(Request.RawUrl)**起作用。任何想法发生了什么? –

+0

@这我我做了同样的事情和它的工作正常使用server.transfer,你有没有访问给定的链接,给他们的详细信息 希望它的帮助你 –

+0

@ this-Me你有没有分配相应的密钥对值到childpage的asp.net控件,即runat = server –

0

在我的情况下使用了两个按钮,用于从设置文化母版页母版页,然后在母版页的代码中使用此代码behid:

protected void IdiomButton_Click(object sender, ImageClickEventArgs e) 
    { 
     ImageButton theButton = (ImageButton)sender; 
     Session["culture"] = theButton.ID == "ItalianButton" ? CultureInfo.CreateSpecificCulture("it-IT") : CultureInfo.CreateSpecificCulture("en-US"); 
     Response.Redirect(Request.RawUrl); 
    } 

然后在每个子页我用:

protected override void InitializeCulture() 
    { 
     if (Session["culture"] != null) 
      System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(Session["culture"].ToString()); 
     else 
      System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("it-IT"); 
    }