通过更改下拉列表值vb.net来更改屏幕上的标签

时间:2015-11-10 19:10:55

标签: html asp.net vb.net

我正在使用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 Label应检索有关的相关信息他来自数据库专栏,例如他的名字,年龄和数据库中表格的相关电影。

但是,我很难找到如何通过点击下拉列表中的值来检索此信息。

该表名为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下拉列表中单击的项目更改标签值并从数据库中检索数据

2 个答案:

答案 0 :(得分:1)

您可以为ddlActors的SelectedIndexChanged事件连接一个事件处理程序:

Protected Sub ddlActors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlClient.SelectedIndexChanged

    Dim intActorID As Integer = CInt(ddlActors.SelectedItem.Value)

    'Use intActorID as a Parameter value for a query for the other information you need from the database.

End Sub

答案 1 :(得分:1)

您必须将查询更改为(根据需要进行调整):

"SELECT * FROM tblActors WHERE ActorEnabled=@EN AND ActorID=@AID"

并按照您对ActorEnabled的方式设置ActorID参数,但使用NoAlias在其响应中所做的操作:

Dim intActorID As Integer = CInt(ddlActors.SelectedItem.Value)
myCommand.Parameters.Add(New SqlParameter("@AID", intActorID ))
相关问题