我正在尝试制作 2 个下拉列表。一个改变主题,另一个改变 UI 文化。改变主题的效果很好,但改变文化存在问题。
第一次运行时,我只能在 InitializeCulture() 函数中设置文化,但在下拉列表中更改选择时,我无法重新设置它。
这是来自我的解决方案:
这在 Web.config
<profile defaultProvider="MyProfileProvider">
<providers>
<add name="MyProfileProvider" connectionStringName="MyMembershipCon" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
<properties>
<add name="Language" type="string"/>
<add name="Theme" type="string"/>
</properties>
</profile>
这是 AdminPage.aspx.cs:
public partial class AdminPage : System.Web.UI.Page
{
string lang;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
if(Session["Theme"]!=null)
{
DropDownList1.SelectedValue = Session["Theme"].ToString();
}
if (Session["Language"] != null)
{
DropDownList2.SelectedValue = Session["Language"].ToString();
}
}
}
protected void Page_PreInit(object sender, EventArgs e)
{
if(Session["Theme"]==null)
{
if(HttpContext.Current.Profile["Theme"].ToString()!="")
{
Session["Theme"] = HttpContext.Current.Profile["Theme"];
}
else
{
HttpContext.Current.Profile["Theme"] = "Theme1";
Session["Theme"] = "Theme1";
Page.Theme = "Theme1";
}
}
if (Session["Language"] == null)
{
if (HttpContext.Current.Profile["Language"].ToString() != "")
{
Session["Language"] = HttpContext.Current.Profile["Language"];
}
else
{
HttpContext.Current.Profile["Language"] = "ar-EG";
Session["Language"] = "ar-EG";
}
}
lang = Session["Language"].ToString();
Page.Theme = Session["Theme"].ToString();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Session["Theme"] = DropDownList1.SelectedValue;
HttpContext.Current.Profile["Theme"] = DropDownList1.SelectedValue;
Response.Redirect(Request.Url.AbsolutePath);
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Session["Language"] = DropDownList2.SelectedValue;
HttpContext.Current.Profile["Language"] = DropDownList2.SelectedValue;
Response.Redirect(Request.Url.AbsolutePath);
}
protected override void InitializeCulture()
{
lang = "ar-EG";
if (Session["Language"] != null)
{
lang = Session["Language"].ToString();
}
Page.UICulture = lang;
Page.Culture = lang;
base.InitializeCulture();
}
}