用javascript為DropDownList控制項下拉式選擇添加一個Item至定義索引位置

來源:互聯網
上載者:User

用Javascript為DropDownList控制項下拉式選擇添加一個Item至定義索引位置。
準備資料,建立一個對象,將是儲存DropDownList控制項每個Item資料。 複製代碼 代碼如下: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

在.aspx放置一個DropDownList控制項複製代碼 代碼如下:<asp:DropDownList ID="DropDownListCatalog" runat="server"></asp:DropDownList>

在.aspx.vb綁定資料複製代碼 代碼如下: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 = "新聞頻道"
cls.Add(cl)
cl = New Catalog()
cl.ID = 2
cl.Name = "體育頻道"
cls.Add(cl)
cl = New Catalog()
cl.ID = 3
cl.Name = "軍事頻道"
cls.Add(cl)
cl = New Catalog()
cl.ID = 4
cl.Name = "教育頻道"
cls.Add(cl)
Return cls
End Function
End Class

準備資料與環境後,寫Javascript: 複製代碼 代碼如下:window.onload = function () {
var catalog = document.getElementById("<%=DropDownListCatalog.ClientID%>");
var obj = document.createElement("option")
obj.text = "請選擇..."
obj.value = 0
catalog.options.insertBefore(obj, catalog.options[0]);
}

DEMO:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.