以上是网站发展的截图...
我们有一个DropDownList控件,并在其的SelectedIndexChanged它回传,然后我们改变网站的文化,然后将其装入相应的资源文件。
DropDownList的ASP.NET code
< ASP:DropDownList的ID =ddlLanguage=服务器的CssClass =选择语言的AutoPostBack =真正的>
< ASP:列表项值=EN-US文本=英语标题=/图片/ Flag_USA.gif>< / ASP:列表项>
< ASP:列表项值=IT-IT文本=意大利语称号=/图片/ Flag_Italian.gif>< / ASP:列表项>
< ASP:列表项值=FR-FR文本=法语称号=/图片/ Flag_French.gif>< / ASP:列表项>
< / ASP:DropDownList的>
继承所有的网页
通用类
使用系统;
使用的System.Web;
使用的System.Threading;
使用System.Globalization;
公共类languagebase:System.Web.UI.Page
{
保护覆盖无效InitializeCulture()
{
尝试
{
String语言code =请求[ctl00 $ ucMenu $ ddlLanguage]; //语言下拉控制前端
如果(!语言code.IsNullOrEmpty())
{
setCulture(语言code);从下拉//设置文化语言
。Request.Cookies时[语言code]值=语言code; //更新请求的Cookie从下拉语言
SetCookies(语言code);从下拉//设置cookie的语言
}
}
赶上(例外前)
{
setCulture(EN-US); //设置默认语言
。Request.Cookies时[语言code]值=EN-US; //更新请求Cookie的语言为默认
SetCookies(EN-US); //设置默认语言
}
base.InitializeCulture();
}
私有静态无效setCulture(字符串LanguageValue)
{
Thread.CurrentThread.CurrentUICulture =新的CultureInfo(LanguageValue);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageValue);
}
公共无效SetCookies(字符串strLanguage)
{
System.Web.HttpContext.Current.Response.Cookies [语言code]值= strLanguage。
。System.Web.HttpContext.Current.Response.Cookies [语言code]过期= DateTime.Now.AddDays(15);
}
}
扩展方法(只是为了信息)
公共静态布尔IsNullOrEmpty(此字符串原创)
{
返回string.IsNullOrEmpty(原件);
}
示例控制在其本地化资源没有加载
<李>
< IMG SRC =/图片/我的-listing.pngALT =ALIGN =absmiddle/>< ASP:超链接ID =hlnkMyProperties=服务器元:的ResourceKey =hlnkMyProperties&GT ;< / ASP:超链接>
< /李>
问题手头
在这里,如果我重新加载页面在浏览器的地址栏,然后资源加载正确。 我只是成像它是如何可能的是,某些控件具有英语资源和一些切换时的文化有着古老的意大利的资源?
我希望我已经解释得很好。
解决方案我改变了HyperLink控件以下语法,瞧!它的工作了。
<李>
< IMG SRC =/图片/我的-listing.pngALT =ALIGN =absmiddle/><一个ID =hlnkMyProperties=服务器><%= GetLocalResourceObject(hlnkMyProperties )%>&所述; / a取代;
< /李>
Above is the screenshot of the site in development...
We have a DropdownList control and on its SelectedIndexChanged it postbacks, and we then change the site culture and it then loads the respective resources files.
DropDownList ASP.NET Code
<asp:DropDownList ID="ddlLanguage" runat="server" CssClass="select-language" AutoPostBack="true">
<asp:ListItem Value="en-US" Text="English" title="/images/Flag_USA.gif"></asp:ListItem>
<asp:ListItem Value="it-IT" Text="Italiano" title="/images/Flag_Italian.gif"></asp:ListItem>
<asp:ListItem Value="fr-FR" Text="Française" title="/images/Flag_French.gif"></asp:ListItem>
</asp:DropDownList>
Common class inherited by all of the web pages
using System;
using System.Web;
using System.Threading;
using System.Globalization;
public class languagebase : System.Web.UI.Page
{
protected override void InitializeCulture()
{
try
{
string LanguageCode = Request["ctl00$ucMenu$ddlLanguage"]; // Language Drop Down Control in Front End
if (!LanguageCode.IsNullOrEmpty())
{
setCulture(LanguageCode); // Set Culture language from drop down
Request.Cookies["LanguageCode"].Value = LanguageCode; // Update REQUEST Cookie language from drop down
SetCookies(LanguageCode); // Set Cookie language from drop down
}
}
catch(Exception ex)
{
setCulture("en-US"); // Set default language
Request.Cookies["LanguageCode"].Value = "en-US"; // Update REQUEST Cookie language to default
SetCookies("en-US"); // Set default language
}
base.InitializeCulture();
}
private static void setCulture(string LanguageValue)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageValue);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageValue);
}
public void SetCookies(string strLanguage)
{
System.Web.HttpContext.Current.Response.Cookies["LanguageCode"].Value = strLanguage;
System.Web.HttpContext.Current.Response.Cookies["LanguageCode"].Expires = DateTime.Now.AddDays(15);
}
}
Extension Method (just for sake of info)
public static Boolean IsNullOrEmpty(this String original)
{
return string.IsNullOrEmpty(original);
}
Example Control on which Localized Resource is not loading
<li>
<img src="/images/my-listing.png" alt="" align="absmiddle" /><asp:HyperLink ID="hlnkMyProperties" runat="server" meta:resourcekey="hlnkMyProperties"></asp:HyperLink>
</li>
Problem at hand
Here, if I reload the page from the browser's addressbar then resources are loaded correctly. I am just imaging how it is possible that the certain controls have English resources and some have old Italian resources when switching the culture?
I hope I have explained it well.
解决方案I changed the HyperLink control to the following syntax and voila! It's working now.
<li>
<img src="/images/my-listing.png" alt="" align="absmiddle" /><a ID="hlnkMyProperties" runat="server"><%= GetLocalResourceObject("hlnkMyProperties")%></a>
</li>