【问题标题】:I want to add Values in 2nd dropdown list after checking tht selective index of 1st frop down in vb.net在检查 vb.net 中第一个下拉列表的选定索引后,我想在第二个下拉列表中添加值
【发布时间】:2011-12-26 06:52:59
【问题描述】:

基本上我有一个有 2 个下拉菜单的表单。在第一个下拉列表中,我选择了城市类别。选择城市后,我想根据第一个下拉菜单中选择的第一个值更新第二个下拉值。我做过这样的, 这会在第一个下拉列表中添加值:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim s As String = "connection String"
    sqlconn = New SqlConnection(s)


    sqlCmd = New SqlCommand("select DISTINCT City from usrRegister_aunthentication", sqlconn)
    If sqlconn.State = Data.ConnectionState.Closed Then
        sqlconn.Open()
    End If

    dataReader = sqlCmd.ExecuteReader()
    While dataReader.Read
        Citydropdown.Items.Add(dataReader("City").ToString())
    End While

    dataReader.Close()

    If sqlconn.State = Data.ConnectionState.Open Then
        sqlconn.Close()
    End If
End Sub

现在假设我选择了一个城市,它应该更新第二个下拉列表,因为我已经这样做了:

Protected Sub Citydropdown_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Citydropdown.Load
        Dim CityID As Integer = Convert.ToInt32(Citydropdown.SelectedValue.ToString())
        FillStates(CityID)

    End Sub


 Private Sub FillStates(ByVal CityID As Integer)
        Dim strConn As String = "server=.;database=Opex;integrated security=true;pooling=false;"
        Dim con As New SqlConnection(strConn)
        Dim cmd As New SqlCommand()
        cmd.Connection = con
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "sql statement"
        Dim objDs As New DataSet()
        Dim dAdapter As New SqlDataAdapter()
        dAdapter.SelectCommand = cmd
        con.Open()
        dAdapter.Fill(objDs)
        con.Close()
        If objDs.Tables(0).Rows.Count > 0 Then
            'HallsDropDown.DataSource = objDs.Tables(0)
            'HallsDropDown.DataTextField = "Theater_Name"
            'HallsDropDown.DataBind()
            'HallsDropDown.Items.Insert(0, "--Select--")
        Else
            Response.Write("No states found")
        End If
    End Sub
End Class

我的 XML 如下。任何人都可以通过从第一个下拉列表中选择索引来帮助我在第二个下拉列表中添加值吗?

<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <table style="height:50px;">
    <tr>
    <td>
    <asp:Label Text="Select your City" runat="server" ID="lblcity"></asp:Label>
    </td>
    <td>
    <asp:DropDownList ID="Citydropdown"   runat="server" />
    </td>
        </tr>
    <tr>
    <td>
    <asp:label runat="server" id="lblHals" Text="Select Your Halls" ></asp:label>  
    </td>
    <td>
    <asp:DropDownList ID="HallsDropDown" runat="server" />
    </td>

    </tr>

    </table>
    </form>
</body>
</html>

【问题讨论】:

  • @AVD 我已经更新了问题你能帮帮我吗

标签: asp.net vb.net drop-down-menu


【解决方案1】:

您将需要打开自动回发并告诉它在下拉列表的选定索引更改时执行什么:

<asp:DropDownList ID="Citydropdown" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Citydropdown_Load">
</asp:DropDownList>

另外,请确保在 Page_Load 方法中添加回发检查,这样您就不会每次都重新加载城市下拉列表:

If Not IsPostBack
    Dim s As String = "connection String"
    sqlconn = New SqlConnection(s)


    sqlCmd = New SqlCommand("select DISTINCT City from usrRegister_aunthentication", sqlconn)
    If sqlconn.State = Data.ConnectionState.Closed Then
        sqlconn.Open()
    End If

    dataReader = sqlCmd.ExecuteReader()
    While dataReader.Read
        Citydropdown.Items.Add(dataReader("City").ToString())
    End While

    dataReader.Close()

    If sqlconn.State = Data.ConnectionState.Open Then
        sqlconn.Close()
    End If
End If

【讨论】:

    【解决方案2】:

    使用这种级联的最佳方式是使用网络服务来绑定您的下拉菜单并通过您的 javascript/jquery 代码调用该网络服务。

    <script type="text/javascript" src="JQuery/jquery-1.6.1.min.js"></script>
    
    <script type="text/javascript">
        $(document).ready(function() {
    
            $("#ddlCity").change(function() {
                $("#ddlState").html("");
                var CityID = $("#ddlCity option:selected").val();
    
                $.getJSON('ServerResponse.aspx?CityID=' + CityID, function(states) {
    
                    $.each(states, function() {
    
                        $("#ddlState").append($("<option></option>").val(this['StateCode']).html(this['StateName']));
    
                    });
                });
    
            });
        });
    </script>  
    

    你可以参考的链接: http://beyondrelational.com/blogs/niladribiswas/archive/2011/07/29/cascading-drop-down-example-country-state-and-city-in-jquery.aspx

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-05
      • 2019-11-24
      • 1970-01-01
      相关资源
      最近更新 更多