【发布时间】:2015-11-10 19:10:55
【问题描述】:
我正在使用 VB.NET 开发一个 asp.net 应用程序,目前有一个下拉列表,其中包含我从数据库中检索到的 2 个值。我正在选择表中列名 ActorEnabled 值设置为 true 的所有值。
这个动作的代码如下
Private Sub SelectActors()
Dim dt As New DataTable
Using con As New SqlConnection(ConnectionString)
Dim SelectAll As String = "SELECT * FROM tblActors WHERE ActorEnabled=@EN"
Dim myCommand As SqlCommand
myCommand = New SqlCommand(SelectAll, con)
myCommand.Parameters.Add(New SqlParameter("@EN", True))
Dim da As New SqlDataAdapter
da.SelectCommand = myCommand
da.Fill(dt)
da.Dispose()
End Using
If dt.Rows.Count > 0 Then 'if there are more than 0 rows in datatable
ddlActors.DataSource = dt 'drop down list gets data from data table
ddlActors.DataTextField = "ActorName" 'text field is shown to user which gets information from column name ActorName
ddlActors.DataValueField = "ActorID" 'value field is field on backend which allows text field to be filled in
ddlActors.DataBind() 'bind data to source
End If
End Sub
我的问题是我希望页面上的内容根据下拉列表中选择的值进行更改,即如果从下拉列表中选择值 Sylvester Stallone,则 asp 标签应检索有关的相关信息他来自数据库列,例如他的姓名、年龄和来自数据库表的相关电影。
但是,我很难找出如何通过单击下拉列表中的值来检索此信息。
该表称为 tblActors,具有以下列
ActorID (PK) int
ActorName varchar(50)
ActorEnabled bit
ActorAge int
如果有帮助,下面是我目前正在使用的 html 代码
<asp:DropDownList ID="ddlActors" runat="server" AutoPostBack="true"></asp:DropDownList>
<div class="outerImages">
<table class="tblImages">
<tr><td><asp:Image runat="server" ID="tblActImg" /></></td></tr>
<tr><td><asp:Label runat="server" ID="lblActorName">ActorName</asp:Label></td></tr>
</table>
</div>
感谢任何帮助
TLDR; 如何根据在 VB.NET 中单击下拉列表的项目更改标签值并从数据库中检索数据
【问题讨论】: