Search code examples
asp.netvisual-web-developer

Empty data template for DropDownList in Visual Web Developer


With GridViews one can specify an emptydatatemplate to fill an area of a gridview with some description of why there is no data to display.

Can this be done with a drop-down list? I have an empty one which isn't populated until the user creates something for it to display, how can I prefill it with some data? I have tried manually adding a list item but the databinding overwrites it with nothing.


Solution

  • If you want to enter an empty field and have it selected, just do the following after you do your databind:

            ddMyDropDown.Items.Insert(0, new ListItem(String.Empty, String.Empty));
            ddMyDropDown.SelectedIndex = 0;