Monday, 8 April 2013

Dynamically bind dropdown with months names


Dynamically bind dropdown with months names


ntroduction
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;
/* Please add Globilaxation name Sapece */
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()));       

        }
    }
}
 
 In above code, i just get the datetime format for the current culture, and then add month names to dropdown list.this is simple snippets, but most usefull.thank for reading.

No comments:

Post a Comment