【问题标题】:Change label on screen by changing drop downlist value vb.net通过更改下拉列表值 vb.net 更改屏幕上的标签
【发布时间】: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 中单击下拉列表的项目更改标签值并从数据库中检索数据

【问题讨论】:

    标签: html asp.net vb.net


    【解决方案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 ))
    

    【讨论】:

      【解决方案2】:

      您可以为您的 ddlActor 的 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
      

      【讨论】:

        猜你喜欢
        • 2018-03-08
        • 1970-01-01
        • 1970-01-01
        • 2016-12-29
        • 2019-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多