单击ASP.NET中的任何按钮时,返回当前文化

时间:2015-01-23 01:30:41

标签: c# asp.net globalization uiculture

通过公司页面选择登录页面中的任何语言时,一切正常,但当我点击公司页面中的任何按钮时,它返回 默认语言(英语)。我想用我选择的语言维护它

请帮忙。

  

Corporation.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
        {


          if (!IsPostBack)
            {

                if (Session["language"] != null)
                {
                    string language = Session["language"].ToString();
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);

                }
                string Flag = Session["Flag"].ToString();
                if (Flag == "Y")
                {
                    Session["Flag"] = "N";
                    Server.Transfer(Request.Path);

                }
                Session["Flag"] = "Y";
           }
       }    
  

的Login.aspx

  <div>
             <asp:DropDownList ID="ddlLang" runat="server"AutoPostBack="True"
             OnSelectedIndexChanged="ddlLang_SelectedIndexChanged">
             <asp:ListItem Text="English" Value="en-US"></asp:ListItem>
             <asp:ListItem Text="French" Value="fr-FR"></asp:ListItem>
             <asp:ListItem Text="Spanish" Value="es-ES"></asp:ListItem>
             <asp:ListItem Text="Chinese" Value="zh-SG"></asp:ListItem>
             </asp:DropDownList>
          </div>
  

Login.aspx.cs

  protected void Page_Load(object sender, EventArgs e)
       {
         if (!Page.IsPostBack)
         {
        if (Session["language"] != null)
                {
                    ddlLang.SelectedValue = Session["language"].ToString();
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture                                  (ddlLang.SelectedValue);
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(ddlLang.SelectedValue);
                }
                else
                {
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture                        (ddlLang.SelectedValue);
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(ddlLang.SelectedValue);
                }
               Session["Flag"] = "Y";
     }
   }





protected void ddlLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlLang.SelectedValue == "fr-FR")
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
                Session["language"] = ddlLang.SelectedValue;
                Server.Transfer(Request.Path);
            }
            if (ddlLang.SelectedValue == "zh-SG")
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("zh-SG");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-SG");
                Session["language"] = ddlLang.SelectedValue;
                Server.Transfer(Request.Path);
            }
            if (ddlLang.SelectedValue == "es-ES")
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("es-ES");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-ES");
                Session["language"] = ddlLang.SelectedValue;
                Server.Transfer(Request.Path);
            }
            else
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                Session["language"] = ddlLang.SelectedValue;
                Server.Transfer(Request.Path);
            }

        }

1 个答案:

答案 0 :(得分:1)

我认为这是一个简单的错误

如果(!IsPostBack)在Corporation.aspx.cs中,将仅在首次加载时执行。 所以基本上所有设置都只在第一次加载时完成。

点击页面上的某个按钮后会发出一个回复,这将是一个Page_Load。但这一次,因为它的回发你的语言代码没有执行...