Reputation: 13616
How can I set programatically default text in DropDownListControl?Do I must define ValueField with default text?
Upvotes: 1
Views: 219
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
Reputation: 2689
ListItem item =new ListItem();
item.Text = "Select . . .";
item.Value = "Select";
item.Selected = true;
dropdownlist1.Items.Add(item);
Upvotes: 2
Reputation: 85046
You can do something like this:
myDdl.Items.Insert(0, "Default text");
Upvotes: 2