asp.net dropdownlist bound database data

Source: Internet
Author: User

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>

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.