ASP tutorial. NET DropDownList bound database tutorial data
Definitions and usage
The DropDownList control is used to create a drop-down list.
Each optional option in the DropDownList control is defined by the ListItem element!
Tip: This control supports data binding!
Property
Property describes. NET
Index number 1.0 of the SelectedIndex option
OnSelectedIndexChanged the name of the function to be executed when the index of the selected item is changed. 1.0
Runat stipulates that the control is a server control. Must be set to ' server '. 1.0
<%@ Page language=vb debug=true%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.OLEDB"%>
<script runat=server>
Sub Page_Load (ByVal Sender as Object, ByVal E as EventArgs)
If not IsPostBack Then
Dim Dbconn as OleDbConnection
Dim DbCommand as OleDbDataAdapter
Dim Dspagedata as New DataSet
' Dbconn = New OleDbConnection (_
"Provider=Microsoft.Jet.OLEDB.4.0" _
' & ' DATA source= ' _
' & Server.MapPath ("Employeedatabase.mdb;")
' Dbconn = New OleDbConnection (_
"Provider=Microsoft.Jet.OLEDB.4.0" _
' & ' DATA source= ' _
' & ' C:inetpubwwwrootemployeedatabase.mdb;
Dbconn = New OleDbConnection (_
"Provider=Microsoft.Jet.OLEDB.4.0;" _
& "DATA source=" _
& MapPath ("Employeedatabase.mdb;")
DbCommand = New OleDbDataAdapter _
("Select LastName &", ' & FirstName "_
& "As EmpName, ID" _
& "From Employee" _
& "ORDER by LastName, FirstName", Dbconn)
Dbcommand.fill (Dspagedata, _
"Employee")
Ddlemps Tutorial. DataSource = _
Dspagedata.tables ("Employee"). DefaultView
Ddlemps.databind ()
End If
End Sub
</SCRIPT>
<HTML>
<HEAD>
<title>connecting to an Access database</title>
</HEAD>
<body leftmargin= ">"
<form runat= "Server" >
<BR><BR>
<asp:dropdownlist
Id= "Ddlemps"
Datatextfield= "EmpName"
Datavaluefield= "ID"
runat= "Server"
/>
</form>
</BODY>
</HTML>
DropdownList
In this case, we declare a DropDownList control in the. aspx file. It then creates an event handle that displays text and selected items in the Label control when the Click event occurs.
<%@ Page language=vb debug=true%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.OLEDB"%>
<script runat=server>
Sub Page_Load (ByVal Sender as Object, ByVal E as EventArgs)
If not IsPostBack Then
Dim Dbconn as OleDbConnection
Dim DbCommand as OleDbDataAdapter
Dim Dspagedata as New DataSet
Dbconn = New OleDbConnection (_
"Provider=Microsoft.Jet.OLEDB.4.0;" _
& "DATA source=" _
& Server.MapPath ("Employeedatabase.mdb;")
DbCommand = New OleDbDataAdapter _
("Select ID, FirstName" _
& "From Employee" _
& "ORDER by FirstName", Dbconn)
Dbcommand.fill (Dspagedata, _
"Employee")
Ddlemployees.datasource = _
Dspagedata.tables ("Employee"). DefaultView
Ddlemployees.databind ()
End If
End Sub
Sub SubmitBtn_Click (Sender as Object, E as EventArgs)
Lbldataselected.text = "Selected Text:" _
& DdlEmployees.SelectedItem.Text _
& "<br>selected Value:" _
& DdlEmployees.SelectedItem.Value _
& "<br>selected Index:" _
& Ddlemployees.selectedindex
End Sub
</SCRIPT>
<HTML>
<HEAD>
<title>binding Database Data to a DropDownList control</title>
</HEAD>
<body leftmargin= ">"
<form runat= "Server" >
<BR><BR>
<asp:label
Id= "Lbldataselected"
runat= "Server"
/>
<BR><BR>
<asp:dropdownlist
Id= "Ddlemployees"
Datatextfield= "FirstName"
Datavaluefield= "ID"
runat= "Server"
/>
<BR><BR>
<asp:button
Id= "Butok"
Text= "OK"
Type= "Submit"
onclick= "SubmitBtn_Click"
runat= "Server"
/>
</form>
</BODY>
</HTML>