2009-11-29 19 views
7

ho il controllo DropDownList in cui gli elenchi dei prodotti sono provenienti da database diAdd to DataBound DropDownList

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

Ma voglio aggiungere alla voce di elenco dall'inizio 1 più "ALL" .. Come posso aggiungere questo uno.

Grazie!

+1

Sembra essere un duplicato di questa domanda? -> http://stackoverflow.com/questions/679128/add-empty-item-to-drop-listdown-of-custom-objects-in-c – JohnIdol

risposta

18

Per aggiungere una nuova voce di elenco a DropDownList, nella finestra Proprietà, fare clic sui puntini di sospensione nella proprietà Items. Aggiungi una nuova voce di elenco con testo "TUTTI" & Valore -1.

Oppure si può aggiungere l'elemento della lista con l'aggiunta di questo markup al DropDownList:

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

Impostare del DropDownList AppendDataBoundItems=True

+5

+1 per AppendDataBoundItems, non conoscevo quella proprietà. – Canavar

+2

Sì, omettendo quello potrebbe davvero mistificarti, eh? – DOK

4

Con il metodo Items.Insert è possibile aggiungere un elemento ad un indice specifico:

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

assicurarsi di disattivare l'viewstate di DropDownList1 in modo che non mantiene ogni resultset dal db!