请教各位大神:两个dropdownlist联动,一个静态,一个动态,动态的事件跑偏。。。
两个下拉列表,一个是lstCls,另一个是lstSubCls
lstCls的列表项来自数据库,是静态绑定的;lstSubCls的列表项根据lstCls的点选动态改变,内容也来自数据库。
其实是很简单的,但写完后发现lstCls的第一项不起作用:加载后,选其他的列表项,lstSubCls可以联动,但选第一项的话,lstSubCls的内容就被清空。另外,选lstCls后,在lstSubCls的内容已正确填充的情况下,点选lstSubCls的列表项,事件触发的是lstCls的onselectindexchanged。
代码如下,请帮忙分析一下问题出在哪,谢谢!
<asp:DropDownList ID="lstCls" runat ="server" AutoPostBack ="True" Width ="200px" DataSourceID="dataProductCls" DataTextField="Class" DataValueField="ID"/> <br /> <asp:DropDownList ID="lstSubCls" runat ="server" Width ="200" AutoPostBack ="true" />
Protected Sub lstCls_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lstCls.SelectedIndexChanged Dim strSelected As String = lstCls.SelectedItem.Text dataProductSubCls.SelectParameters.Clear() dataProductSubCls.SelectParameters.Add("Selected", TypeCode.String, strSelected) dataProductSubCls.SelectCommand = "SELECT ProductCls.Class, ProductSubCls.ID, ProductSubCls.SubClass FROM ProductSubCls " & "INNER JOIN ProductCls ON ProductSubCls.Class = ProductCls.ID " & "WHERE ProductCls.Class = @Selected" lstSubCls.Enabled = True lstSubCls.DataSourceID = "dataProductSubCls" lstSubCls.DataTextField = "SubClass" lstSubCls.DataValueField = "ID" lstSubCls.DataBind() End Sub Protected Sub lstSubCls_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lstSubCls.SelectedIndexChanged Dim strSelected As String = lstSubCls.SelectedItem.Text sqlComm = New SqlCommand("SELECT Pic, Alt FROM ProductSubCls WHERE SubClass = @Selected", sqlConn) sqlComm.Parameters.Add("@Selected", Data.SqlDbType.NVarChar, 50) sqlComm.Parameters("@Selected").Value = strSelected Try sqlConn.Open() reader = sqlComm.ExecuteReader If reader.Read Then ...'将内容附给其他控件 Else ...'执行其他代码 End If Catch ex As Exception Response.Write (ex.Message) Finally sqlConn.Close() End Try End Sub