All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
数据库死锁导致网站站点访问不了之解决方案_[SQL,Server教程]
第一次用.net2.0,LOGIN登陆控件的困惑和解决方法_[Asp.Net教程]
javascript,动态添加表格行_JavaScript技术_编程技术
千万级数据分页之二---一个简单的自定义分页控件_[Asp.Net教程]
Linq,to,SQL,Dynamic,动态查询_[Asp.Net教程]
全面接触SQL语法(2)_[SQL,Server教程]
用C#实现中文验证码_[Asp.Net教程]
在搜索结果出来之前页面显示“等待中...”的做法_[Asp.Net教程]
ASP.NET,2.0高级数据处理之使用参数_.net资料_编程技术
DropDownList无限级分类(灵活控制显示形式)_[Asp.Net教程]
Reading number is top 10 pictures
The hot big eye big breast beauty2
狗狗与主人神同步1
这才是真正的人体艺术5
西游四格漫画(二)
美丽的桂林风光2
So beauty, will let you spray blood10
恶搞漫画2
50个至今影响世界的德国发明
接财神,大吉大利,财源滚滚来
男人帮杂志里的惹火性感美女2
Download software ranking
WebService在.NET中的实战应用教学视频 → 第2集
Kung fu panda - the secret of the teacher
Boxer vs Yellow4
功夫熊猫2(下集)
SP4 for SQL2000
虚拟机汉化软件
Call Of Duty2
Unix video tutorial10
网络管理员第三版
C#COM编程指南
delv published in(发表于) 2014/1/6 8:47:35 Edit(编辑)
ASP.NET,2.0,Language,Swithcer,and,Theme,Swicher,多语_[Asp.Net教程]

ASP.NET,2.0,Language,Swithcer,and,Theme,Swicher,多语_[Asp.Net教程]

ASP.NET 2.0 Language Swithcer and Theme Swicher 多语_[Asp.Net教程]

在ASP.NET 2.0 中提供多语言转换和多样式主题转换功能,两种实现形式比较类似,所以放在一起说明一下。
1. Language switcher 多语言转换
在Quick Start Tutorial 中,介绍了如何存储和应用用户选择的语言。一般是用一个DropDownList展示支持的语言,供用户选择,通常是放在masterpage 里面,将用户选择的语言存储起来 这里用了ASP.NET 2.0的Profile,当然也可以存在cookie session 或者querystring里。在页面里重写InitializeCulture 方法,使用用户之前选择的语言。因为设置语言的操作 (这里是SelectedIndexChanged事件)发生在InitializeCulture 时间后面,所以在设置操作完成后为了使的当前页面也马上生效,需要做个重转向,以从新加载本页面,触发InitializeCulture 事件。下面使quickstart中的部分代码,注意红色部分。因为有的页面地址后面可能还存在queystring,所以个人觉得红色代码部分最好用Response.Redirect(Request.Url.PathAndQuery);代替。
protected void DropDownLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
string SelectedLanguage = DropDownLanguage.SelectedValue.ToString();
//Save selected user language in profile
Profile.SetPropertyValue("PreferredCulture", SelectedLanguage);


//Force re-initialization of the page to fire InitializeCulture()
Response.Redirect(Request.Url.LocalPath);
}
protected override void InitializeCulture()
{
// override virtual method InitializeCulture() to check if profile contains a user language setting
string UserCulture = Profile.GetPropertyValue("PreferredCulture").ToString();
if ( UserCulture != "")
{
// there is a user language setting in the profile: switch to it
Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
}
}
为了减少代码的重复,一般会自定义一个customer base page类,使它继承Page类,然后在自定义的页基类中重新InitializeCulture方法。最后把你的每个页面继承自你的自定义页面基类。这样你就不需要每个页面都重写InitializeCulture方法了。

但是上面这个方法还是不是很爽,因为每添加一个页面都要去修改后置代码,来继承自定义页基类。
我们注意到,在InitializeCulture方法中实际上只是修改了当前线程的Culture和UICulture。那么可不可以在一个全局的事件中,比如Application的某个事件,来修改这两个属性呢?很早以前我这么试过,在Application的BeginRequest事件触发时来实现InitializeCulture 的细节,类似于下面代码:
void Application_BeginRequest(object sender, EventArgs e)
{
string lang = string.Empty;//default to the invariant culture
lang = Profile.PreferredCulture;
if (string.IsNullOrEmpty(lang))
{
lang = string.Empty;
}
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
}
注意红色部分应用其他方式取代,因为在beginrequest触发阶段,profile对象还没有被asp.net创建。可以用cookies取代。
我记得当时这么做后,语言设置后并不起作用,当时认为在全局事件中处理,可能到后来还是会被覆盖掉,所以可能不行。所以当时还是用了 InitializeCulture方法。今天在asp.net论坛里看到有人如此实现了,
void Application_BeginRequest(Object sender, EventArgs e){
string lang = string.Empty;//default to the invariant culture
HttpCookie cookie = Request.Cookies["DropDownName"];


if (cookie != null && cookie.Value != null)
lang = Request.Form[cookie.Value];


Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
}




所以觉得当时可能哪里没有设置好,于是又试了一次,原来是页面头指令<%@ Page UICulture="auto" Culture="auto" %>的原因,如果在页面中设置了UICulture和Culture后,它们就会覆盖掉在全局中的设置。去掉之后,全局设置起作用了。看来页面中的culture的设置会覆盖全局的设置,而页面中InitializeCulture方法(确切说是一切支持该方法的控件)的设置会覆盖页面的设置。其实在Page类中InitializeCulture方法的默认实现是空的,因此再将页面头指令 UICulture="auto" Culture="auto" 去掉后,Global中的设置就起作用了。
另外,如果很想使用Profile(像我一样)来存储用户的选择,那就不能在beginrequest阶段来处理了,我是在PreRequestHandlerExecute事件触发时处理:
void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
string lang = string.Empty;//default to the invariant culture

lang = Profile.PreferredCulture;
if (string.IsNullOrEmpty(lang))
{
lang = string.Empty;
}
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
}
这个时候Profile已经被创建了,所以可以使用了。
2. 多样式主题转换 Theme switcher
这篇文章讲了Theme的切换,觉得形式上和语言的切换很类似。他使用了HttpModule,我觉得直接放在Global.asax文件里对应的事件处理发放下就可以了,说到底都是一样的。他的存储采用了cookie,我还时觉得用Profile好,既然提供了就用呗,Profile应该是有缓存的吧,所以性能应该不是问题。


出处:厚积而勃发 BLOG







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.