Michael
Michael

Reputation: 13616

set programatically default text in DropDownListControl

How can I set programatically default text in DropDownListControl?Do I must define ValueField with default text?

Upvotes: 1

Views: 219

Answers (3)

Brissles
Brissles

Reputation: 3881

You can programatically set the default text (i.e. the default selected item) in a number of ways:

1) dd.SelectedIndex = 0; // by using known index of a DropDownItem, where 0 is your index

2) dd.Items[0].Selected = true; // by setting Selected = true; on an item at a known index, where 0 is your index

3) dd.SelectedIndex = dd.Items.IndexOf(dd.Items.FindByText("my item")); // by using known Text value of a DropDownItem, where "my item" is the known text

Assuming your DropDownList is named dd

Upvotes: 2

Mubarek
Mubarek

Reputation: 2689

ListItem item =new ListItem();
item.Text = "Select . . .";
item.Value = "Select";
item.Selected = true;

dropdownlist1.Items.Add(item);

Upvotes: 2

Abe Miessler
Abe Miessler

Reputation: 85046

You can do something like this:

myDdl.Items.Insert(0, "Default text");

Upvotes: 2

Related Questions