Tuesday, September 22, 2009

Use of Culture in asp.net

This sample code shows use of culture in asp.net


Design:

<form id="form1" runat="server">
<div>
Cultures: <asp:DropDownList id="ddlCulture" DataTextField="DisplayName"
DataValueField="Name" DataSourceID="GetCultureInfo" Runat="server" />
<asp:ObjectDataSource id="GetCultureInfo" TypeName="System.Globalization.CultureInfo"
SelectMethod="GetCultures" Runat="server">
<SelectParameters>
<asp:Parameter Name="types" DefaultValue="SpecificCultures" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:Button id="btnSelect" Text="Select" Runat="server" onclick="btnSelect_Click"></asp:Button><br />
Date:<asp:Label id="lblDate" Runat="server" /><br />
Price : <asp:Label id="lblPrice" Runat="server" />
</div>
</form>



Web.Config:

<profile>
<properties>
<add
name="UserCulture"
defaultValue="en-US" />
<add
name="UserUICulture"
defaultValue="en" />
</properties>
</profile>


Code Behind:

protected override void InitializeCulture()
{
Culture = Profile.UserCulture;
UICulture = Profile.UserUICulture;
}

protected void btnSelect_Click(object sender, EventArgs e)
{
Profile.UserCulture = ddlCulture.SelectedValue;
Profile.UserUICulture = ddlCulture.SelectedValue;
//Response.Redirect(Request.Path);
}

void Page_PreRender()
{
lblDate.Text = DateTime.Now.ToString("D");
lblPrice.Text = (512.33m).ToString("c");
}

No comments: