Dynamically Bind DropDown String,Data in ASP.NET

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()));       

        }
    }
}
  ..................................................................
                (or)
Bind String Data Directly
String Name="phani";

   DropDownList1.Items.Add(new ListItem(Name.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.

0 comments:

Post a Comment