所选索引已更改未触发。下拉列表正在初始化,可以滚动到下一个值,显示下一个选定索引,但没有DropDownList1_SelectedIndexChanged启用子例程。
Default.aspx的
<asp:DropDownList ID="DropDownList1" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true" runat="server"></asp:DropDownList>
Default.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim connStr, cmdStr As String
Dim ds As New DataSet
connStr = "connection string works"
Try
cmdStr = "SELECT [idt],[col1] FROM [test];"
Using conn As New SqlConnection(connStr)
Using cmd As New SqlCommand(cmdStr, conn)
conn.Open()
cmd.ExecuteNonQuery()
Using da As New SqlDataAdapter(cmd)
da.Fill(ds)
DropDownList1.DataSource = ds.Tables(0)
DropDownList1.DataTextField = "idt"
DropDownList1.DataValueField = "col1"
DropDownList1.DataBind()
End Using
cmd.Dispose()
conn.Close()
conn.Dispose()
End Using
End Using
Catch ex As Exception
TextBox1.Text = ex.Message
End Try
End if
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim datetimesi As String = "01-01-2014 00:00:00"
Dim col1si As String = "0"
Dim col2si As String = "0"
Dim col3si As String = "0"
Dim connStr, cmdStr As String
connStr = "connection string works"
Try
cmdStr = "SELECT [datetime],[col1],[col2],[col3] FROM [test] WHERE [idt]=@idt;"
Using conn As New SqlConnection(connStr)
Using cmd As New SqlCommand(cmdStr, conn)
conn.Open()
cmd.Parameters.AddWithValue("@idt", DropDownList1.SelectedItem.Text)
cmd.ExecuteNonQuery()
Using rdr = cmd.ExecuteReader()
While rdr.Read()
datetimesi = rdr(0)
col1si = rdr(1)
col2si = rdr(2)
col3si = rdr(3)
End While
TextBox10.Text = DropDownList1.Text
TextBox11.Text = datetimesi
TextBox12.Text = col1si
TextBox13.Text = col2si
TextBox14.Text = col3si
End Using
cmd.Dispose()
conn.Close()
conn.Dispose()
End Using
End Using
Catch ex As Exception
TextBox1.Text = "ddlSelectedIndexChanged:" & ex.Message
End Try
End Sub
答案 0 :(得分:0)
我更改了OnSelectedIndexChanged =&#34; DropDownList1_SelectedIndexChanged&#34;到OnTextChanged =&#34; DropDownList1_TextChanged&#34;
和
受保护的Sub DropDownList1_TextChanged(ByVal sender As Object,ByVal e As System.EventArgs)Handles DropDownList1.TextChanged
并且有效。