我正在尝试制作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();
}
}