I have a login page where the user can select his language through a radio button, after login the user is redirected to Default.aspx where I'm using the below method to set the page culture:
<%@ 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>
How can I pass the selected language from the login page to this method in Default.aspx page? I tried to pass it through session but I got this error:
System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Web.SessionState.HttpSessionState.this[string].get returned null.
Because this method happens in early stages before any control or session is initiated, that's why it returns null. Any idea how can I pass the selected culture to this method?
I found the answer, I managed to pass the selected culture info using Query String:
in my login page I have those two radio buttons and a button:
in the code behind the button OnClick:
then in Default.aspx page I have this code:
I tried to use Cookie also but it returns null as well like the session, so the query string is the only way that worked for me.