Propagting在母版页的资源文化变革的内容网页 [英] Propagting the resource culture change in master page to the content web pages

查看:151
本文介绍了Propagting在母版页的资源文化变革的内容网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个菜鸟,当涉及到Web应用程序。但我尽我所能使用ASP.NET 2.0和遗憾的长期职位了解它。

I'm a noob when it comes to web applications. But i'm trying my best to learn it using ASP.NET 2.0 and sorry for the long post.

我有一个母版页(M1)和3个不同的内容pagesC1,C2,C3,基本上使用的母版页M1在内容占位符尽显其各自的内容。

I have a master page(M1) and 3 different content pagesC1,C2,C3 which basically use the master page M1 for filling its respective contents in the content placeholder.

所有网络的形式本地化和适当的语言资源字符串在资源(XML)文件前说:Resource.en-US.xml,Resource.de-DE.xml等on.Finally的资源被称为在建立适当的电流文化和当前的UICulture之后,code。

All the web-forms are localized and appropriate language resource strings are added in the resource (xml) files ex: Resource.en-US.xml,Resource.de-DE.xml and so on.Finally the resources are referred in the code after setting up the appropriate current culture and current uiculture.

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:形象。每当用户点击所需语言LinkBut​​ton的,内容页面控件将显示对应选择的文化的全部内容的字符串。

Now comes the question, I have included an option of changing the display language in the master page available to the user as an asp:linkbutton with an asp:image. Whenever the user clicks the linkbutton for the desired language, the content page controls shall display the whole content strings corresponding to the culture selected.


  1. 我如何实现这一目标?

  1. How do i achieve this ?

我一定要实现Session变量,包括所选择的
    语言 ?或存储的Cookie也将做的工作?

Do i have to implement Session variables to include the selected language ? Or storing in Cookie would also do the job ?

我试图

在母版页加载事件。我试图调用一个方法的 SetCultureSpecificInformation ,基本上设置CurrentThread的Culture和UICulture属性和存储在会话变量中选定的语言。

On master page load event. I tried calling a method SetCultureSpecificInformation, which basically sets the culture and uiculture properties of CurrentThread and store the selected language inside a session variable.

另外关于ASP类似的执行:LinkBut​​ton的的OnClick事件处理程序。在这种情况下,修改会话变量。

Also a similar implementation on the asp:linkbutton OnClick eventhandler. In this case it modifies the session variable.

最后引用会话变量的内容网页OnPage_Load事件。

Finally refer the session variable on the content web page OnPage_Load event.

但不知何故,上面的办法没有产生预期的效果。语言的切换是不相符的。有任何人谁可以帮助我实现同样的足够好的设计方法。

But somehow the above approach is not yielding desired results. The switching of language is not consistent. Anyone out there who can help me out with a good enough design approach for implementing the same.

在此先感谢

推荐答案

添加的Global.asax 文件:写这篇文章code的

Add Global.asax file: write this piece of code

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


和母版页


And on Masterpage page

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

博客文章:创建多个语言的网站在asp.net C#

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

这篇关于Propagting在母版页的资源文化变革的内容网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆