我有一个登录页面,用户可以通过单选按钮选择他的语言,登录后用户被重定向到Default.aspx,我使用以下方法设置页面文化:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"%>
<%@ Import Namespace="System.Resources" %>
<% @Import Namespace="System.Globalization" %>
<% @Import Namespace="System.Threading" %>
<script runat=server>
protected override void InitializeCulture()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Session["lang"].ToString());
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Session["lang"].ToString());
base.InitializeCulture();
}
</script>
如何在Default.aspx页面中将所选语言从登录页面传递给此方法?我试图通过会话传递它,但我收到了这个错误:
System.NullReferenceException:'对象引用未设置为对象的实例。' System.Web.SessionState.HttpSessionState.this [string] .get返回null。
因为此方法在任何控件或会话启动之前的早期阶段发生,所以它返回null。我知道如何将选定的文化传递给这种方法?
答案 0 :(得分:0)
您的代码中的“lang”加载到Session中的哪个位置?
我相信这告诉你在SessionState中不存在“lang”而SessionState本身不可用。
答案 1 :(得分:0)
您是将其设置为默认设置还是允许用户选择它?
如果前者只是将它放在App.xaml.cs中:
[assembly: NeutralResourcesLanguage("en-US")]
如果你担心线程文化,也可以这样做:
var culture = CultureInfo.CreateSpecificCulture("en-US");
if (Thread.CurrentThread.CurrentCulture.Name != "en-US")
{
var culture = CultureInfo.CreateSpecificCulture("en-US");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
}
答案 2 :(得分:0)
Change Culture of a webpage as in MSDN
<%@ Page Language="C#" uiculture="auto" %> <%@ Import Namespace="System.Threading" %> <%@ Import Namespace="System.Globalization" %> <script runat="server"> protected override void InitializeCulture() { if (Request.Form["ListBox1"] != null) { String selectedLanguage = Request.Form["ListBox1"]; UICulture = selectedLanguage ; Culture = selectedLanguage ; Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage); Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage); } base.InitializeCulture(); } </script>
答案 3 :(得分:0)
我找到答案,我设法使用查询字符串传递所选的文化信息:
在我的登录页面中,我有两个单选按钮和一个按钮:
<asp:RadioButton ID="RadioButton1" runat="server" Text="ar-AE" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="en-US" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
在按钮OnClick后面的代码中:
protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
Response.Redirect("Default.aspx?language=" + RadioButton1.Text);
}
else
{
Response.Redirect("Default.aspx?language=" + RadioButton2.Text);
}
}
然后在Default.aspx页面中我有这段代码:
<% @Import Namespace="System.Resources" %>
<% @Import Namespace="System.Globalization" %>
<% @Import Namespace="System.Threading" %>
<script runat=server>
protected override void InitializeCulture()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.QueryString["language"].ToString());
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.QueryString["language"].ToString());
base.InitializeCulture();
}
</script>
我也尝试使用Cookie,但它也像会话一样返回null,因此查询字符串是唯一对我有用的方法。