Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 5th, 2011, 05:45 PM
Authorized User
 
Join Date: Nov 2010
Posts: 63
Thanks: 3
Thanked 0 Times in 0 Posts
Default Globalization and date formats

Hello
I have a server in the US. I want to have users from the US getting seeing dates retrieved from the database in the mm/dd/yyyy format, but users from Europe should see the same dates in the dd/mm/yyyy format. How can I do this?
Thanks.
 
Old February 6th, 2011, 05:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You can set the culture of the ASP.NET thread to match the user's culture. Here's a quick overview:

http://www.codeproject.com/Kb/aspnet...ivekTakur.aspx

If "in Europe" actually means by physical location (rather than determined by OS, supported language or user preference) you also need an IP service to translate a user's IP address to a country or region code.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
avidan (February 13th, 2011)
 
Old October 27th, 2011, 06:11 PM
Registered User
 
Join Date: Jun 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Imar,
If we were to Globalize your example in PlanetWrox and place a language selection (Itemlist control and button) in the Sidebar (located in the Masterpage), how could we get the Override ( protected override void InitializeCulture()) to work. From what I have seen, it cannot reside in the masterpage. The control for the language selection would naturally look better if it was in the sidebar. As a concept, how could I resolve the issue?
I ran a simple example of a globalized ASP page with its resource files and it wasn't difficult.
Code:
<%@ Page Language="C#" uiculture="auto" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Globalization" %>
<script runat="server">
protected override void InitializeCulture()
{
    if (Request.Form["ListBox1"] != null)
    {
        String selectedLanguage = Request.Form["ListBox1"];
        UICulture = selectedLanguage ;
        Culture = selectedLanguage ;

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
    }
    base.InitializeCulture();
}
</script>
<html>
<head><title>Set Culture</title></head>
<body>
    <form id="form1" runat="server">
    <div>
            <asp:ListBox id="ListBox1" runat="server">
                <asp:ListItem value="en-US" 
                    selected="True">English</asp:ListItem>
                <asp:ListItem value="es-MX">Español</asp:ListItem>
                <asp:ListItem value="de-DE">Deutsch</asp:ListItem>
            </asp:ListBox><br />
            <asp:Button id="Button1" runat="server" Text="<%$ Resources:Resource, Button1 %>"/>
            <br />
            <asp:Label id="Label1" runat="server" Text="<%$ Resources:Resource, Label1 %>" />
    </div>
    <asp:Button ID="Button2" runat="server" Text="<%$ Resources:Resource, Button2 %>" />

    </form>
</body>
</html>
 
Old October 29th, 2011, 09:38 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I would use the exact same mechanism as the theme switcher does: store the selected culture in a cookie and set it in the BasePage.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Formats carumuga SQL Server 2000 4 January 2nd, 2007 07:20 PM
XML Date Formats tracyamgray XSLT 5 July 12th, 2006 10:17 PM
Trouble with date formats DrewMills Access VBA 3 March 7th, 2005 06:22 PM
SQL and Date Formats al_bllinky Access 3 August 28th, 2003 08:45 AM
Date formats in the forum Crispin Horsfield Forum and Wrox.com Feedback 1 August 3rd, 2003 08:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.