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

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;




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

<option value="1">januarioption>

<option value="2">februarioption>

<option value="3">maartoption>

<option value="4">apriloption>

<option value="5">meioption>

<option value="6">junioption>

<option value="7">julioption>

<option value="8">augustusoption>

<option value="9">septemberoption>

<option value="10">oktoberoption>

<option value="11">novemberoption>

<option value="12">decemberoption>