Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am using below code it work fine for translating , in one form i have "AutoPostBack" on dropdown when the page auto post back it changes the language of the aspform from french to english again.

how can i prevent changing the languag on postback


ASP.NET
<asp:DropDownList ID="ddlanguage" runat="server"
                         onselectedindexchanged="DropDownList1_SelectedIndexChanged"
                         AutoPostBack="True">
                         <asp:ListItem Value="en">English</asp:ListItem>
                         <asp:ListItem Value="ar">Arabic</asp:ListItem>
                     </asp:DropDownList>
               <asp:Button  ID="Button2" runat="server" Height="23px"
                Text="<%$Resources:Resource, name%>"  Width="100px"
                         onclick="Button1_Click" />
    <asp:Button  ID="Button2" runat="server" Height="23px"
                Text="<%$Resources:Resource, passwrd%>"  Width="100px"
                         onclick="Button1_Click" />


global.asax

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




c#

C#
using System.Globalization;
using System.Threading;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
 if (!Page.IsPostBack){
  if (Session["ddindex"] != null) {
   ddlanguage.SelectedValue = Session["ddindex"].ToString();
   ddlanguage.SelectedIndex = Convert.ToInt32(Session["ddindex"].ToString());
  }
  else{
    ddlanguage.SelectedValue = Thread.CurrentThread.CurrentCulture.Name;
  }
 }
}
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;
 }
 
 Server.Transfer(Request.Path);
}
}
Posted
Updated 7-Dec-13 19:55pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900