Populate Dropdown List-box from a database using ASP.NET Server Control DropDownList Server Control using the SQLClient class against SQL Server and using a datareader<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" debug="False" trace="False" strict="True" %> <%@ Import Namespace="System.Data.SQLClient" %> <script language=VB runat=server>Sub Page_Load(Sender As Object, E As EventArgs) Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs")) Dim mySQL as string = "Select au_id, au_lname, au_fname from Authors" Dim myCmd as New SQLCommand(mySQL,myConnection) myConnection.Open()MyDropDownList.DataSource = myCmd.ExecuteReader() myDropDownList.DataBind() myConnection.Close() End Sub</script> <html> <head><title>DropListBox using SQLDataReader</title></head><body> <asp:dropdownlist id="MyDropDownList" DataValueField = "au_lname" DataTextField = "au_id" runat="server"/> </body> </html>DropDownList Server Control using the OleDb class against Access db and using a datareader <%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" Trace="False" Debug="False" Strict="True" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.Data.OLEDB" %> <script language="VB" runat="server"> Sub Page_Load(Src As Object, E As EventArgs) Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs")) Dim mySQL as string = "SELECT ProductID, productName FROM products" Dim myCmd as New OLEDBCommand(mySQL,myConnection) myConnection.Open()myDropDown.DataSource = myCmd.ExecuteReader(system.data.CommandBehavior.CloseConnection) myDropDown.DataBind() myConnection.Close() End Sub </script> <html> <head><title>Dropdown List box using OLEDB</Title></head> <body> <asp:dropdownlist id="myDropDown" datatextfield="ProductName" dataTextValue="Productid" runat="server" /> </body> </html>DropDownList Server Control using the OleDb class against Access db and using a SQLDataAdapter<%@ Page EnableSessionState="False" EnableViewState="False" debug="False" trace="False" strict="True" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SQLClient" %><script language=VB runat=server>Sub Page_Load(Sender As Object, E As EventArgs) Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs")) Dim ds as DataSet = new DataSet() dim adapter as SqlDataAdapter = new SqlDataAdapter("Select au_id, au_lname, au_fname from Authors", myConnection) adapter.Fill(ds,"Authors")MyDropDownList.DataSource = ds.Tables("Authors").DefaultView'This is what will be displayed in the dropdown box MyDropDownList.DataTextField = "au_id"'This is what will be the Value sent in the form values MyDropDownList.DataValueField = "au_lname"'This binds the data to the dropdownlist control. MyDropDownList.DataBind()End Sub</script> <html><head></head> <body><asp:dropdownlist id="MyDropDownList" runat="server"/> </body></html>Web.Config file that stores the connection string<configuration> <appSettings> <add key="DSN_northwind" value="DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\inetpub\wwwroot\nwind.mdb"/> <add key="DSN_pubs" value="server=(local)\NetSDK;uid=sa;pwd=;database=pubs" /> </appSettings> <system.web> <customErrors mode="Off" /> </system.web> </configuration>--------------------------------------------------------------------------------
我上层两兄弟提出了两种可行的访法。我再来一种。用dataReader dim con as .. dim com as .. dim dr as datareader .. dr=com.excuteReader() while(dr.read()) ddlName.add(dr(""),dr("")) wend ..
ddlUnit.DataValueField = "单位代码"
ddlUnit.DataTextField = "单位名称"
ddlUnit.DataBind()
Populate Dropdown List-box from a database using ASP.NET Server Control
DropDownList Server Control using the SQLClient class against SQL Server and using a datareader<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" debug="False" trace="False" strict="True" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script language=VB runat=server>Sub Page_Load(Sender As Object, E As EventArgs)
Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))
Dim mySQL as string = "Select au_id, au_lname, au_fname from Authors"
Dim myCmd as New SQLCommand(mySQL,myConnection)
myConnection.Open()MyDropDownList.DataSource = myCmd.ExecuteReader()
myDropDownList.DataBind()
myConnection.Close()
End Sub</script>
<html>
<head><title>DropListBox using SQLDataReader</title></head><body>
<asp:dropdownlist id="MyDropDownList" DataValueField = "au_lname" DataTextField = "au_id" runat="server"/>
</body>
</html>DropDownList Server Control using the OleDb class against Access db and using a datareader
<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" Trace="False" Debug="False" Strict="True" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs) Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))
Dim mySQL as string = "SELECT ProductID, productName FROM products"
Dim myCmd as New OLEDBCommand(mySQL,myConnection)
myConnection.Open()myDropDown.DataSource = myCmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
myDropDown.DataBind()
myConnection.Close()
End Sub
</script>
<html>
<head><title>Dropdown List box using OLEDB</Title></head>
<body>
<asp:dropdownlist id="myDropDown" datatextfield="ProductName" dataTextValue="Productid" runat="server" />
</body>
</html>DropDownList Server Control using the OleDb class against Access db and using a SQLDataAdapter<%@ Page EnableSessionState="False" EnableViewState="False" debug="False" trace="False" strict="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %><script language=VB runat=server>Sub Page_Load(Sender As Object, E As EventArgs) Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))
Dim ds as DataSet = new DataSet()
dim adapter as SqlDataAdapter = new SqlDataAdapter("Select au_id, au_lname, au_fname from Authors", myConnection)
adapter.Fill(ds,"Authors")MyDropDownList.DataSource = ds.Tables("Authors").DefaultView'This is what will be displayed in the dropdown box
MyDropDownList.DataTextField = "au_id"'This is what will be the Value sent in the form values
MyDropDownList.DataValueField = "au_lname"'This binds the data to the dropdownlist control.
MyDropDownList.DataBind()End Sub</script>
<html><head></head>
<body><asp:dropdownlist id="MyDropDownList" runat="server"/>
</body></html>Web.Config file that stores the connection string<configuration>
<appSettings>
<add key="DSN_northwind" value="DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\inetpub\wwwroot\nwind.mdb"/>
<add key="DSN_pubs" value="server=(local)\NetSDK;uid=sa;pwd=;database=pubs" />
</appSettings>
<system.web>
<customErrors mode="Off" />
</system.web>
</configuration>--------------------------------------------------------------------------------
Adapter.Fill(table);
for(int i=0;o<table.Columns.Count;i++)
{Dropdownlist1.Items.Add(new ListItem(table.Columns[i].Caption);)}
就可以将表中的字段显示到dropdownlist中了
ddlName.DataValueField = "后台操纵的值";
ddlName.DataTextField = "实际显示的值";
ddlName.DataBind()
dim con as ..
dim com as ..
dim dr as datareader
..
dr=com.excuteReader()
while(dr.read())
ddlName.add(dr(""),dr(""))
wend
..