Use JavaScript to add an item to the DropDownList control Drop-down selection to define an index location _ practical Tips

Source: Internet
Author: User
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:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.