【问题标题】:How to make a select option change options in another select如何在另一个选择中进行选择选项更改选项
【发布时间】:2011-10-03 08:46:18
【问题描述】:

我有两个下拉菜单。 SelCurrentManuf 和 selCurrentModel。我希望 selCurrentModel 中的选项根据 selCurrentManuf 中选择的选项进行更改。我该怎么做?

<asp:DropDownList runat="server" ID="selCurrentManuf"></asp:DropDownList>
<asp:DropDownList runat="server" ID="selCurrentModel"></asp:DropDownList>

这就是我当前填充 selCurrentModel 的方式

Public Sub PopulateCurrentModel()
        Dim mySelectQuery As String = "SELECT * FROM Model where ManufID = "+ selCurrentManuf.Text+";"
        Dim myConnection As New MySqlConnection(Session("localConn"))
        Dim myCommand As New MySqlCommand(mySelectQuery, myConnection)

        myConnection.Open()
        Dim myReader As MySqlDataReader
        myReader = myCommand.ExecuteReader()

        While myReader.Read
            Dim newListItem As New ListItem
            newListItem.Value = myReader.GetString("Modelid")
            newListItem.Text = myReader.GetString("desc")
            selCurrentModel.Items.Add(newListItem)
        End While

        myReader.Close()
        myConnection.Close()
    End Sub

但它只填充第一个选择的manuf,之后不会改变

Private Sub selCurrentManuf_SelectedIndexChanged(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles selCurrentManuf.SelectedIndexChanged

       PopulateCurrentModel()
End Sub

【问题讨论】:

  • 这个问题与.net、ajax、vb.net、vb、asp等无关,只涉及javascript。
  • this example 你打算做什么?或this one?
  • 啊,是的,这就是我想要做的,但是示例在 c# 中,我想我使用的是 vb
  • @Uku 这听起来像是我个人会使用 AJAX 解决的问题。
  • 我认为也想知道选择selcurrentmauf时如何刷新selcurrentmodel下降? span>

标签: javascript asp.net ajax vb.net


【解决方案1】:

selCurrentManuf.Text 更改为selCurrentManuf.SelectedItem.Value

请注意:您有一个 SQL 注入安全漏洞。请搜索 SQL 注入并修复。

【讨论】:

  • 你现有的代码有selCurrentManuf,没有更多代码我不能说你为什么会得到这个错误。错字?你有没有改变别的东西?
  • 抱歉,我在不同的页面上尝试过。但即使它在我显示的页面上,当用户放下框并更改模型时,另一个下拉列表的值不会改变,它不会再次运行该函数
  • 您是否参与了 ​​on change 事件? on change 事件应该调用上面的代码。
  • 不适用于我的 on change,我的 on change 看起来正确吗?
  • 在循环前添加selCurrentModel.Items.Clear()
【解决方案2】:

这就是我们所需要的! autopostback="true" !!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 2018-04-01
    • 2020-12-28
    • 2018-06-29
    • 2021-03-27
    相关资源
    最近更新 更多