Binding the dropdown values through code

This is something very basic and simple, most of the beginners like me encounters. You might wonder how to bind the list elements manually into the dropdown list instead binding the data source in C#.net. If you have ended up in the same, its is the right place to figure it out. Here I go,

protected void Page_Load(object sender, EventArgs e) //values are populated when the page is requested by the client
{
string[] names = {“Sharmili”,”Deepika”,”Rathinam”,”Anitha”}; //Get the dropdown list elements to be populated

foreach (string i in names) //when you use foreach, you need not bother about array index
{
DropDownList1.Items.Add(i);
}

//The other way to bind the values one by one is shown below
//DropDownList1.Items.Add(“Sharmili”);
//DropDownList1.Items.Add(“Deepika”);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s