Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 804
AlexC Avatar asked Nov 29 '09 20:11

AlexC


2 Answers

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

like image 109
DOK Avatar answered Sep 23 '22 00:09

DOK


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

DropDownList1.Items.Insert(0, new ListItem("ALL", "ALL"));
like image 37
Canavar Avatar answered Sep 26 '22 00:09

Canavar