使用母版页在ASP.NET应用程序上切换语言
发布时间:2020-12-16 07:31:49 所属栏目:asp.Net 来源:网络整理
导读:我正在使用Visual Studio 2008和ASP.NET来构建Web应用程序.我的大多数网页都基于一个母版页.该母版页包含三个用作语言切换的按钮链接; Click事件处理程序如下所示: protected void lbuLangEN_Click(object sender,EventArgs e){ this.SwitchLanguage(string
我正在使用Visual Studio 2008和ASP.NET来构建Web应用程序.我的大多数网页都基于一个母版页.该母版页包含三个用作语言切换的按钮链接; Click事件处理程序如下所示:
protected void lbuLangEN_Click(object sender,EventArgs e) { this.SwitchLanguage(string.Empty); } protected void lbuLangES_Click(object sender,EventArgs e) { this.SwitchLanguage("es"); } 然后我有我的私有方法SwitchLanguage: private void SwitchLanguage(string culture) { Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture); } 据我所知,这应该足以使基于我的母版页的页面表现出本地化的行为,即根据文化从适当的本地resx文件获取其资源.但是,它不起作用.它们总是以西班牙语出现,这是我浏览器的默认语言.我在SwitchLanguage的入口和出口点设置了一些跟踪消息,显然当前线程没有改变它的文化信息:每次调用SwitchLanguage时,当前线程的文化是“es-ES”,无论我的代码是什么刚刚设定. 我的代码或我采用的方法有问题吗?谢谢. 解决方法
您可以在Global.asax中使用OnAcquireRequestState来更改文化.它在加载会话后立即调用,但在任何母版页事件之前调用.
因此,在按钮事件处理程序中,您将会话变量设置为所需的当前区域性.然后,您重定向到当前页面.然后,Global.asax事件可以在加载母版页之前选择新语言: protected void OnAcquireRequestState(object sender,EventArgs e) { string cultureName = "en-GB"; if (HttpContext.Current.Session != null && HttpContext.Current.Session["CultureName"] is string) cultureName = HttpContext.Current.Session["CultureName"]; if (Thread.CurrentThread.CurrentUICulture.Name == cultureName) return; Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName); Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – (.NET Core 1.0 \u0026\u0026 net46)
- asp.net – 插入后在实体框架中获取记录ID
- MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWI
- asp.net-mvc – Asp.Net MVC Html助手扩展
- asp.net-mvc – 使用ASP.NET MVC测试驱动的开发 – 从哪里开
- asp.net-mvc-2 – Asp.net Mvc显示String的模板,但现在每个
- asp.net-mvc – 如何让ASP.NET MVC在IIS 6中运行?
- 在ASP.NET标记中“使用”等效的命名空间
- asp.net-mvc – ASP.NET MVC可配置验证
- asp.net-core-2.1 – 如何在asp.net Core 2.1.1中为Identit
推荐文章
站长推荐
- asp.net-core-mvc – asp.net核心,包括共享内容文
- asp.net – 关闭Application Insights
- asp.net-mvc-5 – 为什么我不能以种子用户身份登
- asp.net-mvc-3 – FluentValidation – 验证跨多
- Fluent验证比较字段字符串格式args
- ASP.NET Core使用NLog记录日志
- asp.net – MathJax – 文件无法加载:/extensio
- asp.net-mvc – 使用EditorFor时向ModelMetadata
- asp.net-mvc – RedirectToAction重定向查询参数
- asp.net – IIS如何知道它是服务于一个网站还是一
热点阅读