Saturday 6 April 2013

Dynamically bind dropdown with months names


Dynamically bind dropdown with months names


Introduction
Through this article, i will show to how to Bind item Dynamically to Dropdownlist in ASP.NET.
Implementation
Just place a drop downlist into your web page, then add  following code to bind items to dropdown list within the page load event.
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack )
        {
            DD_Monthbind();
        }
    }

    private void DD_Monthbind()
    {
        DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
        for (int i = 1; i < 13; i++)
        {       
            DropDownList1.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));       

        }
    }
}

No comments:

Post a Comment