[英]VB asp.net : i want to add some data into dropdownlist from database
i want to add some data into dropdownlist from database and don't have duplicate data, so i did this ~我想从数据库中添加一些数据到下拉列表中并且没有重复数据,所以我这样做了~
Dim ads As New Web.UI.WebControls.AccessDataSource
ads.DataFile = "~/app_data/board.mdb"
ads.SelectCommand = "SELECT DISTINCT [photo_species] FROM [phototable]"
Dim dv As DataView = ads.Select(New DataSourceSelectArguments)
For i = 0 To dv.Count - 1
DropDownList1.Items.Add(dv.Item(0).Row("photo_species"))
Next
but when i run the code it shows the same data again and again但是当我运行代码时,它一次又一次地显示相同的数据
Change the 0 in this line:更改此行中的 0:
DropDownList1.Items.Add(dv.Item(0).Row("photo_species"))
to i:对我:
DropDownList1.Items.Add(dv.Item(i).Row("photo_species"))
It is not clear if this is a vb.net (NOT vb6), and it not clear if this is a web page (asp.net), or a desktop only program?不清楚这是 vb.net(不是 vb6),也不清楚这是 web 页面(asp.net)还是桌面程序?
If this is asp.net (web based), then this should work:如果这是 asp.net(基于网络),那么这应该有效:
First, the web markup is this:首先,web 标记是这样的:
<h3>Select Hotel</h3>
<asp:DropDownList ID="DropDownList1" runat="server"
DataValueField="ID"
DataTextField="HotelName" height="30px" Width="206px">
</asp:DropDownList>
Like most drop downs, there are (can be) two columns.像大多数下拉菜单一样,有(可以)两列。
The "hidden" value - in most cases the database PK row ID. “隐藏”值——在大多数情况下是数据库主键行 ID。
And then the display value.然后是显示值。 (hence in above Value field, and text field).
(因此在上面的值字段和文本字段中)。
DataValueField = hidden column - value returned from drop down DataTextField = the display column from the data.table (to display). DataValueField = 隐藏列 - 从下拉列表返回的值 DataTextField = 来自 data.table 的显示列(要显示)。
our code to load is thus this:因此,我们要加载的代码是这样的:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadData()
End If
End Sub
Sub LoadData()
Using conn As New OleDbConnection(My.Settings.AccessDB)
Dim strSQL =
"SELECT ID, Hotelname FROM tblhotelsA ORDER BY HotelName"
Using cmdSQL As New OleDbCommand(strSQL, conn)
conn.Open()
Dim rstData As New DataTable
rstData.Load(cmdSQL.ExecuteReader)
DropDownList1.DataSource = rstData
DropDownList1.DataBind()
' add default please select
DropDownList1.Items.Insert(0, New ListItem("Please Select Hotel", ""))
End Using
End Using
End Sub
And we now get this:我们现在得到这个:
And if in code we need to test/get/look at/grab the selected value?如果在代码中我们需要测试/获取/查看/获取所选值?
Then use this code:然后使用这段代码:
Debug.Print("drop down pk (id) = " & DropDownList1.SelectedItem.Value)
Debug.Print("drop down hotel text = " & DropDownList1.SelectedItem.Text)
The above allows you to get both columns (the hidden one), and the text/display one.以上允许您获得两列(隐藏的一列)和文本/显示一列。
As a general rule, you can also use the.Text property of the drop down list to get the selected value.作为一般规则,您还可以使用下拉列表的 .Text 属性来获取所选值。
So, this also gets the value.所以,这也得到了价值。
Debug.Print("drop down value = " & DropDownList1.Text)
so, above will return the same as DropDownList1.SelectedItem.Value所以,上面将返回与 DropDownList1.SelectedItem.Value 相同的结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.