AlexC
AlexC

Reputation: 9661

Add item to databound DropDownList

I have dropdownlist control where item lists are coming from database

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        DataSourceID="SqlDataSource2" DataTextField="semester" 
        DataValueField="semester">
    </asp:DropDownList>

But I want to add at the beginning 1 list item more "ALL" .. How can I add this one .

Thanks !

Upvotes: 8

Views: 9308

Answers (3)

Kevin
Kevin

Reputation: 21

Be sure to disable the viewstate of DropDownList1 so that it doesn't retain each resultset from the db!

Upvotes: 2

Canavar
Canavar

Reputation: 48088

With Items.Insert method you can add an item at a specific index :

DropDownList1.Items.Insert(0, new ListItem("ALL", "ALL"));

Upvotes: 5

DOK
DOK

Reputation: 32841

To add a new list item to the DropDownList, in the Properties window, click on the ellipses in the Items property. Add a new list item with Text "ALL" & Value -1.

Or you can add the list item by adding this markup to the DropDownList:

<asp:DropDownList ID="categories" runat="server" ...>
    <asp:ListItem Value="-1">
       ALL
    </asp:ListItem>         
</asp:DropDownList>

Set the DropDownList's AppendDataBoundItems=True

Upvotes: 19

Related Questions