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里的代码要怎么改才可以啊??????????????
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里的代码要怎么改才可以啊??????????????
解决方案 »
- 哪里可以买到CMS+博客+社区等整套门户网站解决方案的开源代码!!
- 关于状态问题(一个网站的订单追踪是怎么实现的?)
- onclick="parent.location.href='http://www.microsoft.com';"> 在当窗口中打开,我想在框架中的主窗口打开怎么做啦!
- 请问这两行代码是什么意思?
- 点击BUTTON,弹出对话框,让用户选择Y/N,怎么写代码?
- 需要一个asp.net系统的源码,类似动网新文,收到后立即给分。
- 100分请教一下问题大家来看看!!!顶者就有分!!!一个经典的问题不能错过哦!!!!
- 50难题:不使用服务器控件,如何实现Response.Write()
- 更改IP IIS不可用
- 怎样将dropdownlist中的重复数据只显示一个呢???
- 怎样能随机输出Web服务器控件!!!
- 请问在DataGrid中内容缩略的方法,比如“遵守中华人民共和国有关法律”,显示为“遵守中华人民共和国。。。”
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
else '判断DropDownList1是否不变化,如果变化,重新绑定
if viewstate("DropDownListValue")!=DropDownList1.SelectedItem.Value then
viewstate("DropDownListValue")=DropDownList1.SelectedItem.Value'将变化值重新付给
重新绑定DropDownList1
'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
怎么在每个DropDownList选项里的第一个选项前加一个空白选项啊????