001.
<%@ Import
Namespace
=
"System.Data"
%>
002.
<%@ Import
Namespace
=
"System.Data.OleDb"
%>
003.
<%@ Page Language=
"VB"
%>
004.
<script runat=
"server"
>
005.
006.
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
007.
IF
Not
Page.IsPostBack()
Then
008.
DropDownListDataTable()
009.
DropDownListDataTableRows()
010.
DropDownListSortedList()
011.
DropDownListAddInsertItem()
012.
End
IF
013.
End
Sub
014.
015.
016.
Function
DropDownListDataTable()
017.
Dim
objConn
As
OleDbConnection
018.
Dim
dtAdapter
As
OleDbDataAdapter
019.
Dim
dt
As
New
DataTable
020.
021.
Dim
strConnString
As
String
022.
strConnString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
& _
023.
Server.MapPath(
"database/mydatabase.mdb"
)&
";"
024.
objConn =
New
OleDbConnection(strConnString)
025.
objConn.Open()
026.
027.
Dim
strSQL
As
String
028.
strSQL =
"SELECT * FROM country"
029.
030.
dtAdapter =
New
OleDbDataAdapter(strSQL, objConn)
031.
dtAdapter.Fill(dt)
032.
033.
dtAdapter =
Nothing
034.
objConn.Close()
035.
objConn =
Nothing
036.
037.
038.
With
Me
.myDDL1
039.
.DataSource = dt
040.
.DataTextField =
"CountryName"
041.
.DataValueField =
"CountryCode"
042.
.DataBind()
043.
End
With
044.
045.
046.
myDDL1.SelectedIndex = myDDL1.Items.IndexOf(myDDL1.Items.FindByValue(
"TH"
))
047.
048.
049.
End
Function
050.
051.
052.
Sub
DropDownListDataTableRows()
053.
Dim
dt
As
New
DataTable
054.
Dim
dr
As
DataRow
055.
056.
057.
dt.Columns.Add(
"Sex"
)
058.
dt.Columns.Add(
"SexDesc"
)
059.
060.
061.
dr = dt.NewRow
062.
dr(
"Sex"
) =
"M"
063.
dr(
"SexDesc"
) =
"Man"
064.
dt.Rows.Add(dr)
065.
066.
067.
dr = dt.NewRow
068.
dr(
"Sex"
) =
"W"
069.
dr(
"SexDesc"
) =
"Woman"
070.
dt.Rows.Add(dr)
071.
072.
073.
With
Me
.myDDL2
074.
.DataSource = dt
075.
.DataTextField =
"SexDesc"
076.
.DataValueField =
"Sex"
077.
.DataBind()
078.
End
With
079.
080.
081.
myDDL2.SelectedIndex = myDDL2.Items.IndexOf(myDDL2.Items.FindByValue(
"W"
))
082.
083.
084.
End
Sub
085.
086.
087.
Sub
DropDownListSortedList()
088.
Dim
mySortedList AS
New
SortedList
089.
090.
mySortedList.Add(
"M"
,
"Man"
)
091.
mySortedList.Add(
"W"
,
"Woman"
)
092.
093.
094.
With
Me
.myDDL3
095.
.DataSource = mySortedList
096.
.DataTextField =
"Value"
097.
.DataValueField =
"Key"
098.
.DataBind()
099.
End
With
100.
101.
102.
myDDL3.SelectedIndex = myDDL3.Items.IndexOf(myDDL3.Items.FindByValue(
"W"
))
103.
104.
End
Sub
105.
106.
107.
Sub
DropDownListAddInsertItem()
108.
Dim
mySortedList AS
New
SortedList
109.
110.
mySortedList.Add(
"M"
,
"Man"
)
111.
mySortedList.Add(
"W"
,
"Woman"
)
112.
113.
114.
With
Me
.myDDL4
115.
.DataSource = mySortedList
116.
.DataTextField =
"Value"
117.
.DataValueField =
"Key"
118.
.DataBind()
119.
End
With
120.
121.
122.
Dim
strText,strValue
As
String
123.
124.
125.
strText =
""
126.
strValue =
""
127.
Dim
InsertItem
As
New
ListItem(strText, strValue)
128.
myDDL4.Items.Insert(0, InsertItem)
129.
130.
131.
strText =
"Guy"
132.
strValue =
"G"
133.
Dim
AddItem
As
New
ListItem(strText, strValue)
134.
myDDL4.Items.Add(AddItem)
135.
136.
137.
myDDL4.SelectedIndex = myDDL4.Items.IndexOf(myDDL4.Items.FindByValue(
""
))
138.
139.
End
Sub
140.
141.
Sub
Button1_OnClick(sender as
Object
, e
As
EventArgs)
142.
Me
.lblText1.Text =
Me
.myDDL1.SelectedItem.Value
143.
Me
.lblText2.Text =
Me
.myDDL2.SelectedItem.Value
144.
Me
.lblText3.Text =
Me
.myDDL3.SelectedItem.Value
145.
Me
.lblText4.Text =
Me
.myDDL4.SelectedItem.Value
146.
End
Sub
147.
148.
</script>
149.
<html>
150.
<head>
151.
<title>ThaiCreate.Com ASP.NET - DropDownlist & DataBind</title>
152.
</head>
153.
<body>
154.
<form id=
"form1"
runat=
"server"
>
155.
<asp:DropDownList id=
"myDDL1"
runat=
"server"
></asp:DropDownList>
156.
<asp:DropDownList id=
"myDDL2"
runat=
"server"
></asp:DropDownList>
157.
<asp:DropDownList id=
"myDDL3"
runat=
"server"
></asp:DropDownList>
158.
<asp:DropDownList id=
"myDDL4"
runat=
"server"
></asp:DropDownList>
159.
<asp:Button id=
"Button1"
onclick=
"Button1_OnClick"
runat=
"server"
Text=
"Button"
></asp:Button>
160.
<hr />
161.
<asp:Label id=
"lblText1"
runat=
"server"
></asp:Label><br />
162.
<asp:Label id=
"lblText2"
runat=
"server"
></asp:Label><br />
163.
<asp:Label id=
"lblText3"
runat=
"server"
></asp:Label><br />
164.
<asp:Label id=
"lblText4"
runat=
"server"
></asp:Label><br />
165.
</form>
166.
</body>
167.
</html>