WithFlyingColors
WithFlyingColors

Reputation: 2760

DropDownList and its initial value

How can I set the initial value of a drop downlist say to the word "Empty"..regardless to what i bind to it in the pageload or feed values dynamically

Upvotes: 1

Views: 215

Answers (3)

SMK
SMK

Reputation: 2158

Try This :

DrpDwn_ProductType.Items.Insert(0, new ListItem("-- Empty...", ""));

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460058

You should add this item manually and set the DropDownList's AppendDataBoundItems property to true.

For example (added on ASPX):

<asp:DropDownList ID="DropDownList1" runat="server" 
   AppendDataBoundItems="true" 
   DataSource="myDataSource" 
   DataTextField="TextColumn" 
   DataValueField="IdColumn">
   <asp:ListItem Text="Empty..." Value="0" Selected="True"></asp:ListItem>
</asp:DropDownList>

or in codebehind:

DropDownList1.Items.Add(New ListItem("Empty...", "0", True))
DropDownList1.DataTextField = "TextColumn"
DropDownList1.DataValueField = "IdColumn"
DropDownList1.DataSource = myDataSource
DropDownList1.DataBind()

Upvotes: 2

brenjt
brenjt

Reputation: 16297

Just create an new list item and then add it to the Drop Down. Do it in the Page_load function or data bind.

var emptyvalue = new ListItem("Empty...", "0");
DropDownItem.Items.Add(emptyvalue );

Upvotes: 2

Related Questions