Insert an extra item into Dynamic DropDownList in ASP.NET


I created a drop down list in ASP.NET to display a list of security questions.

I intended to add that “Select a security question” entry at the top.
For that the following small piece of code served my requirement. It adds an extra entry at the top of the drop down list

protected void DropDownList1_DataBound(object sender, EventArgs e)

{

DropDownList ddl = (DropDownList)sender;

ddl.Items.Insert(0, new ListItem("text", "value"));

}

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