Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            conn.Open()
            Dim sql As String
            sql = "SELECT DISTINCT lei FROM baojia"
            Cmd = New System.Data.SqlClient.SqlCommand(sql, conn)
            Dim reader As System.Data.SqlClient.SqlDataReader
            reader = Cmd.ExecuteReader
            DropDownList1.DataSource = reader
            DropDownList1.DataTextField = "lei"
            DropDownList1.DataValueField = "lei"
            DropDownList1.DataBind()
            reader.Close()
            conn.Close()        End If
    End Sub
    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        conn.Open()
        Dim sql As String
        sql = "SELECT DISTINCT name FROM baojia where lei='" & DropDownList1.SelectedItem.Value & "'"
        Cmd = New System.Data.SqlClient.SqlCommand(sql, conn)
        Dim reader As System.Data.SqlClient.SqlDataReader
        reader = Cmd.ExecuteReader
        DropDownList2.DataSource = reader
        DropDownList2.DataTextField = "name"
        DropDownList1.DataValueField = "name"
        DropDownList2.DataBind()
        reader.Close()
        conn.Close()    End Sub    Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
        conn.Open()
        Dim sql As String
        sql = " SELECT DISTINCT chang FROM baojia where lei='" & DropDownList1.SelectedItem.Value & "' and name='" & DropDownList2.SelectedItem.Value & "'"
        Cmd = New System.Data.SqlClient.SqlCommand(sql, conn)
        Dim reader As System.Data.SqlClient.SqlDataReader
        reader = Cmd.ExecuteReader
        DropDownList3.DataSource = reader
        DropDownList3.DataTextField = "chang"
        DropDownList2.DataValueField = "chang"
        DropDownList3.DataBind()
        reader.Close()
        conn.Close()    End Sub
    Private Sub DropDownList3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList3.SelectedIndexChanged
        Try
            Dim rd As System.Data.SqlClient.SqlDataAdapter
            Dim ds As DataSet
            Dim dv As DataView
            Dim sql As String
            conn.Open()
            sql = "SELECT name as '名称',pro as '型号',chang as '厂商',jie as '参数',price as '价格' FROM baojia where lei='" & DropDownList1.SelectedItem.Value & "' and name='" & DropDownList2.SelectedItem.Value & "' and chang='" & DropDownList3.SelectedItem.Value & "'"
            rd = New System.Data.SqlClient.SqlDataAdapter(sql, conn)
            ds = New DataSet
            rd.Fill(ds, "baojia")
            dv = ds.Tables("baojia").DefaultView
            DataGrid1.DataSource = dv
            Dim dd As String = "dddddddd"
            Response.Write("<script language=javascript>" & Chr(13) & "alert('" & dd & "'); " & " </script>")
            DataGrid1.DataBind()
        Catch es As Exception
            Response.Write("<script language=javascript>" & Chr(13) & "alert('" & es.Message & "'); " & " </script>")
        Finally
            conn.Close()
        End Try
  end sub
这个可以实现三层查询了,
选择DropDownList2项时,可以自动选择一个DropDownList3项
但是选择DropDownList1项时,不能自动选择DropDownList2项和DropDownList3项
所以当再去选择DropDownList1项时,DropDownList2和DropDownList3还保留着上次的选项,
所以查询会出错, DropDownList3里的代码要怎么改才可以啊??????????????

解决方案 »

  1.   

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not IsPostBack Then
                conn.Open()
                Dim sql As String
                sql = "SELECT DISTINCT lei FROM baojia"
                Cmd = New System.Data.SqlClient.SqlCommand(sql, conn)
                Dim reader As System.Data.SqlClient.SqlDataReader
                reader = Cmd.ExecuteReader
                DropDownList1.DataSource = reader
                DropDownList1.DataTextField = "lei"
                DropDownList1.DataValueField = "lei"
                DropDownList1.DataBind()
                reader.Close()
                conn.Close()        else
          判断DropDownList1是否不变化,如果变化,重新绑定        End If
        End Sub
      

  2.   

    else后面的代码要怎样写啊????
      

  3.   

    用viewstate("DropDownListValue")保存DropDownList1.SelectedItem.Value   如: viewstate("DropDownListValue")=DropDownList1.SelectedItem.Value 
       else   '判断DropDownList1是否不变化,如果变化,重新绑定
       if viewstate("DropDownListValue")!=DropDownList1.SelectedItem.Value   then 
            viewstate("DropDownListValue")=DropDownList1.SelectedItem.Value'将变化值重新付给 
           
           重新绑定DropDownList1
      

  4.   

    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
            'If DropDownList2.DataTextField = "" Then
            conn.Open()
            Dim sql As String
            sql = "SELECT DISTINCT name FROM baojia where lei='" & DropDownList1.SelectedItem.Value & "'"
            Cmd = New System.Data.SqlClient.SqlCommand(sql, conn)
            Dim reader As System.Data.SqlClient.SqlDataReader
            reader = Cmd.ExecuteReader
            DropDownList2.DataSource = reader
            DropDownList2.DataTextField = "name"
            DropDownList1.DataValueField = "name"
            DropDownList3.DataTextField = ""
            DropDownList2.DataValueField = ""
            DropDownList2.DataBind()
            reader.Close()
            conn.Close()
     end sub
      

  5.   

    问题解决了,
    怎么在每个DropDownList选项里的第一个选项前加一个空白选项啊????