Technolog

Blogging over technologie.
Welcome to Technolog Sign in | Join | Help
in Search

eprogrammer

Fill a dropdownlist using the current UICulture in (ASP.NET)

Just for educational purpose (as for myself as well :) ) I post this code.

Using this, a programmer can use a good practise, that is to use the culture information which is built in, into .NET instead of making that data him/herself.

based upon the following element in web.Config, the list will fill using the correct number of monthnames. It also keeps track of calendars, that have 13 months in some cultures.

<globalization uiCulture="nl-nl"/>

using System;

using System.Collections.Generic;

using System.Web;

using System.Linq;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Threading;

using System.Globalization;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        int monthNumber = 0;

        CultureInfo ci = Thread.CurrentThread.CurrentUICulture;

        var myMonthnames = ci.DateTimeFormat.MonthNames

            .Take(ci.Calendar.GetMonthsInYear(DateTime.Today.Year)).Select(p => new { monthNo = ++monthNumber, monthName = p });

        ddlMonthnames.DataTextField = "monthName";

        ddlMonthnames.DataValueField = "monthNo";

        ddlMonthnames.DataSource = myMonthnames;

        ddlMonthnames.DataBind();

    }

}

<select name="ddlMonthnames" id="ddlMonthnames">

<option value="1">januari</option>

<option value="2">februari</option>

<option value="3">maart</option>

<option value="4">april</option>

<option value="5">mei</option>

<option value="6">juni</option>

<option value="7">juli</option>

<option value="8">augustus</option>

<option value="9">september</option>

<option value="10">oktober</option>

<option value="11">november</option>

<option value="12">december</option>

</select>

Published Thursday, June 18, 2009 12:05 PM by eprogrammer
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

About eprogrammer

Egbert Nierop is enthousiast C# en ASP.NET evangelist . Egbert Nierop is zelfstandige en houdt van projecten met een hoog technologische uitdaging.
Powered by Community Server, by Telligent Systems