Hussein ALSHAMIRI Hussein ALSHAMIRI - 4 months ago 8
ASP.NET (C#) Question

Can't change language using session at runtime ASP.NET

in fact I'm still new to ASP.NET and been trying to build a simple web application containing gridview to retrieve data from database and show it to the user and the user can change the language by pressing on a radio button to change from English to Arabic and vise versa but unfortunately when I choose Arabic when the page load for the first time nothing happens and it keeps the page in English unless and until I press the English radio button.

P.S. Please check the attached file to fully understand my problem.

here's my HTML code

<div>
&nbsp;<asp:RadioButtonList ID="LangRadioButton" AutoPostBack="true" runat="server" RepeatColumns="2" meta:resourcekey="LangRadioButtonResource1"
OnSelectedIndexChanged="LangRadioButton_SelectedIndexChanged">
<asp:ListItem Text="English" Value="en-US" Selected="True"></asp:ListItem>
<asp:ListItem Text="Arabic" Value="ar-YE"></asp:ListItem>
</asp:RadioButtonList>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
BorderWidth="1px" CellPadding="3" CellSpacing="2"
DataSourceID="ObjectDataSource1" DataKeyNames="Id" meta:resourcekey="GridView1Resource1">
<Columns>
<asp:CommandField ShowSelectButton="True" meta:resourcekey="CommandFieldResource1" />
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" meta:resourcekey="BoundFieldResource1" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" meta:resourcekey="BoundFieldResource2" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" meta:resourcekey="BoundFieldResource3" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" DataSourceID="ObjectDataSource2" Height="50px" Width="125px" DataKeyNames="Id"
OnItemDeleted="DetailsView1_ItemDeleted" OnItemInserted="DetailsView1_ItemInserted"
OnItemUpdated="DetailsView1_ItemUpdated" meta:resourcekey="DetailsView1Resource1">
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White"/>
<Fields>
<asp:BoundField DataField="Id" InsertVisible="false" ReadOnly="true" HeaderText="Id" SortExpression="Id" meta:resourcekey="BoundFieldResource4" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" meta:resourcekey="BoundFieldResource5" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" meta:resourcekey="BoundFieldResource6" />
<asp:BoundField DataField="PhoneNo" HeaderText="PhoneNo" SortExpression="PhoneNo" meta:resourcekey="BoundFieldResource7" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" meta:resourcekey="CommandFieldResource2" />
</Fields>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
</asp:DetailsView>
<br />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAllEmployeesBasicDetails" TypeName="CascadingDDL.EmployeeDataAccessLayer"></asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllEmployeesFullDetailsByUd"
TypeName="CascadingDDL.EmployeeDataAccessLayer" DeleteMethod="DeleteEmployee" InsertMethod="InsertEmployee"
UpdateMethod="UpdateEmployee">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="PhoneNo" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Id" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="PhoneNo" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>

</div>


and here's my code-behind

public partial class DetailsviewInUpDe : System.Web.UI.Page
{
protected override void InitializeCulture()
{
if (Session["Lang"] != null)
{
string cultureName = Session["Lang"].ToString();
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
}

base.InitializeCulture();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Lang"] == null)
{
Session["Lang"] = "en-US";
}
}

}
protected void LangRadioButton_SelectedIndexChanged(object sender, EventArgs e)
{
Session["Lang"] = LangRadioButton.SelectedValue.ToString();
}
protected void Page_PreRender(object sender, EventArgs e)
{

if (CultureInfo.CurrentUICulture.Name == "ar-YE")
{
form1.Attributes["dir"] = ((string)GetLocalResourceObject("Dir")).ToString();
}
else
{
form1.Attributes["dir"] = ((string)GetLocalResourceObject("Dir")).ToString();
}

if (GridView1.SelectedRow == null)
{
DetailsView1.Visible = false;
}
else
{
DetailsView1.Visible = true;
}
}

protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
GridView1.DataBind();
GridView1.SelectRow(-1);
}

protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
{
GridView1.DataBind();
GridView1.SelectRow(-1);
}

protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
GridView1.DataBind();
GridView1.SelectRow(-1);
}
}


demo for the encountered problem

Answer

This MSDN article describes, how to change page's culture programmatically. In fact, the InitializeCulture method is invoked much earlier during the page processing than then LangRadioButton_SelectedIndexChanged event handler. So don't use event handler to read the selected value.

Read selected value directly from posted data (Request.Form name/value collection) in the InitializeCulture method this way:

protected override void InitializeCulture()
{
    //read posted value
    if (!String.IsNullOrEmpty(Request.Form["LangRadioButton"])) {
        Session["Lang"] = Request.Form["LangRadioButton"];
    }

    //set default value if it's empty
    if (String.IsNullOrEmpty(Session["Lang"]))
    {
        Session["Lang"] = "en-US";
    }

    string cultureName = Session["Lang"].ToString();
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);

    base.InitializeCulture();
}

Then, delete useless code from methods Page_Load and LangRadioButton_SelectedIndexChanged and that is all ;-)