我解決這個問題經過長時間的搜尋後更改語言。這是答案和您需要的所有代碼。我爲Visual Studio 2010中的母版頁做好了準備。
您可以在頁面加載中使用ispostback。
protected void Page_Load(object sender, EventArgs e)
{
//only does it on non-postback because otherwise the selected
//value will not reach event handler correctly
if (!Page.IsPostBack)
{
dil = Thread.CurrentThread.CurrentCulture.Name;
}
}
那麼之後我們可以添加按鈕,點擊餅乾
protected void Button2_Click(object sender, EventArgs e)
{
dil = "en-US";
//var ci = new CultureInfo(dil); //TO_DO Route culture
//Thread.CurrentThread.CurrentUICulture = ci;
//Thread.CurrentThread.CurrentCulture = ci;
//Session["culture"] = ci;
//Sets the cookie that is to be used by Global.asax
HttpCookie cookie = new HttpCookie("CultureInfo");
cookie.Value = dil;
Response.Cookies.Add(cookie);
//Set the culture and reload the page for immediate effect.
//Future effects are handled by Global.asax
Thread.CurrentThread.CurrentCulture =
new CultureInfo(dil);
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(dil);
Server.Transfer(Request.Path);
}
和最後一個Global.asax文件可以幫助解決這個問題。
//*
Public 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("tr-TR");
System.Threading.Thread.CurrentThread.CurrentCulture = new
System.Globalization.CultureInfo("tr-TR");
}
}
//*
如果您使用的是html標籤而不是.net標籤,則可以使用這些標籤來添加文本控件。
<a><asp:Literal ID="Literal1" runat="server" Text="<%$Resources: PB, Home %>" /></a>
我該如何在Cookie中設置默認語言。我不知道使用cookie! – Handelika
//將cookie設置爲defaultLanguage HttpCookie hc = new HttpCookie(「dil」); hc.Expires = DateTime.Now.AddDays(30); hc.Value =「tr」; HttpContext.Current.Response.Cookies.Add(hc); –
不幸的是我的c#不支持。我使用C#第4版=( – Handelika