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里的代码要怎么改才可以啊??????????????
解决方案 »
- 求助 获取asp.net控件的实际位置或屏幕实际位置
- 调试程序时aspnet_wp.exe: DefaultDomain”已退出
- 问一条sql语句
- 我这样写,她提示我过时,应该怎么修改?
- C#能否实现对webbrowser和IE编程?
- 快来救命,100分求助!有关MDAC的问题!给源码再加100分!
- 关于图片以及文字内容共同上传的小例子,以及通过asp.net实现rss的例子。
- 202.160.180.*这个IP一直访问我的错误页面??
- 在Exe中调用Dll时如何让Dll读入自己的.Config?顶贴有分
- 关于ASP.NET ajax的开发方法问题
- 怎样能随机输出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选项里的第一个选项前加一个空白选项啊????