asp.net ตัวอย่างการใช้ Listbox ของ asp.net โดยดึงค่าจาก database ลงใน listbox
Tag : - - - -
Date :
6 ก.พ. 2548 01:04:27
By :
555
View :
3537
Reply :
1
No. 1
Guest
Code (VB.NET)
Function ListBoxDataTable()
Dim objConn As OleDbConnection
Dim dtAdapter As OleDbDataAdapter
Dim dt As New DataTable
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& _
Server.MapPath("database/mydatabase.mdb")&";"
objConn = New OleDbConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM country"
dtAdapter = New OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
'*** ListBox ***'
With Me.myListBox1
.DataSource = dt
.DataTextField = "CountryName"
.DataValueField = "CountryCode"
.DataBind()
End With
'*** Default Value ***'
myListBox1.SelectedIndex = myListBox1.Items.IndexOf(myListBox1.Items.FindByValue("TH")) '*** By DataValueField ***'
'myListBox1.SelectedIndex = myListBox1.Items.IndexOf(myListBox1.Items.FindByText("Thailand")) '*** By DataTextField ***'
End Function