Use JavaScript to add an item to the DropDownList control Drop-down selection to define the index position.
Prepares the data, creates an object, will be the storage DropDownList control each item data.
Copy Code code as follows:
Imports Microsoft.VisualBasic
Namespace insus.net
Public Class Catalog
Private _id as Integer
Private _name as String
Public Property ID as Integer
Get
return _id
End Get
Set (value as Integer)
_id = value
End Set
End Property
Public Property Name as String
Get
return _name
End Get
Set (value as String)
_name = value
End Set
End Property
End Class
End Namespace
Place a DropDownList control in. aspx:
Copy Code code as follows:
<asp:dropdownlist id= "Dropdownlistcatalog" runat= "Server" ></asp:DropDownList>
binding data in. aspx.vb:
Copy Code code as follows:
Imports insus.net
Partial Class _default
Inherits System.Web.UI.Page
Protected Sub Page_Load (sender as Object, e as EventArgs) Handles Me.load
If not IsPostBack Then
Data_binding ()
End If
End Sub
Private Sub data_binding ()
Me.DropDownListCatalog.DataSource = GetData ()
Me.DropDownListCatalog.DataValueField = "ID"
Me.DropDownListCatalog.DataTextField = "Name"
Me.DropDownListCatalog.DataBind ()
End Sub
Private Function GetData () as List (of Catalog)
Dim CLS as New List (of Catalog)
Dim cl as Catalog = New Catalog ()
Cl.id = 1
Cl. Name = "News Channel"
Cls. ADD (CL)
CL = New Catalog ()
Cl.id = 2
Cl. Name = "Sports channel"
Cls. ADD (CL)
CL = New Catalog ()
Cl.id = 3
Cl. Name = "Military Channel"
Cls. ADD (CL)
CL = New Catalog ()
Cl.id = 4
Cl. Name = "Education channel"
Cls. ADD (CL)
Return CLS
End Function
End Class
write JavaScript after preparing data and environment:
Copy Code code as follows:
Window.onload = function () {
var catalog = document.getElementById ("<%=DropDownListCatalog.ClientID%>");
var obj = document.createelement ("option")
Obj.text = "Please select ..."
Obj.value = 0
Catalog.options.insertBefore (obj, catalog.options[0]);
}
DEMO: