[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 555
  • Last Modified:

How can I pass the Current UICulture & CurrentCulture to another page as a session variable?

In my application, the users can pick a language. I would like to use the selected culture/language as a session variable so that it can be used on the next page.  Should I use a session variable?  Or is there a better way to do this?
.cs file
    protected override void InitializeCulture()
    {
        string lang = Request["Language1"];
        if (lang != null && lang != "")
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
        }
   
    }

.aspx file

<asp:DropDownList ID="Language1"
                    runat="server" AutoPostBack="True"
            meta:resourcekey="Language1Resource1"
            onselectedindexchanged="Language1_SelectedIndexChanged">
            <asp:ListItem Value="en-US" meta:resourcekey="ListItemResource1">Auto</asp:ListItem>
            <asp:ListItem Value="en-US" meta:resourcekey="ListItemResource2">English (US)</asp:ListItem>
            <asp:ListItem Value="es-mx" meta:resourcekey="ListItemResource3">Spanish (MX)</asp:ListItem>
            </asp:DropDownList>  
0
need_code
Asked:
need_code
  • 2
  • 2
1 Solution
 
rajapandian_81Commented:
Yes. You can use Session.

To store a value in session use below code.
Session["lang"] = lang;

To get that session value in other page, use below code
string strLang = Session["lang"].ToString();
0
 
need_codeAuthor Commented:
Thanks.  Now how do I use it to set the Culture information?
0
 
need_codeAuthor Commented:
This is what worked for me.

protected string SessionCulture
    {
        get
        {
            return Session["lang"] as string;
        }
        set
        {
            Session["lang"] = value;
        }
    }

    protected string SelectedCulture
    {
        get
        {
            return Request.Form["Language1"];
        }
    }

    protected override void InitializeCulture()
    {

        string lang = SelectedCulture ?? SessionCulture;

        if (lang != null)
        {
            UICulture = lang;
            Culture = lang;
            SessionCulture = lang;
        }
        base.InitializeCulture();
         
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string lang = SelectedCulture ?? SessionCulture;
            if (lang != null)
            {
                for (int i = 0; i < Language1.Items.Count; i++)
                {
                    ListItem item = Language1.Items[i];
                    if (item.Value.Equals(lang))
                    {
                        Language1.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

On the receiving page I used:

protected override void InitializeCulture()
    {
        if (Session["lang"] != null)
        {
            string lang = Session["lang"] as string;
            UICulture = lang;
            Culture = lang;
        }
        base.InitializeCulture();
    }
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now