DropDownlist doesn't change language - ASP.NET - C# -Culture is not supported. Parameter name: name

510 views Asked by At

I am trying to make 2 dropdown lists. One to change the theme and the other one to change the UI culture. The one that changes the theme works well, but there is a problem changing the culture.

I can set the culture only in InitializeCulture() function when running it for the first time, but I can't re-set it when the selection is changed in the dropdown list.

and this is from my solution :

[my files - Admin page is the name for the page where there are the 2 dropdownlists]

and this in the 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>

and this is 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();
        }

    }
0

There are 0 answers