Home » .NET 4.0 » Add item on the dropdownlist – VB.NET

Add item on the dropdownlist – VB.NET

Markup page

'define control -->
<asp:DropDownList ID="MySelectionDDL" runat="server" 
AutoPostBack="True" AppendDataBoundItems="true" 
DataSourceID="MySelectionDS" DataTextField="Text" DataValueField="Value" 
'define datasource -->
<asp:SqlDataSource ID="MySelectionDS" runat="server" 
ConnectionString="< %$ ConnectionStrings:my_sql_connection %>" 
SelectCommand="SELECT [Text], [Value] FROM [MyTable] ORDER BY [Text]">

Code behind

Protected Sub MySelectionDDLDataBound(ByVal sender As Object, ByVal e As EventArgs)

'add item at the beginning of the list 
MySelectionDDL.Items.Insert(0, New ListItem("Please Select", ""))

'add item at the end of the list (assuming there are 10 records on the list)
MySelectionDDL.Items.Insert(11, New ListItem("Other", "Other"))
End Sub