为什么我的listbox总也选不中
我明明选中了项但ListBox1.Items(l).Selected 依然不等于true
以下是我的代码
<asp:ListBox ID= "ListBox1 " runat= "server " Height= "136px " Width= "168px " SelectionMode= "Multiple "> </asp:ListBox>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Conn As New SqlConnection
Dim Comd As SqlCommand
Dim strsql As String
Dim dset As New DataSet
Dim obj As New jbfx.pub
Dim Dtrd As SqlDataReader
Conn.ConnectionString = obj.Conn_str
Conn.Open()
strsql = "select * from bm where s_sjbm= '00000000 ' "
Comd = New SqlCommand(Trim(strsql), Conn)
Dtrd = Comd.ExecuteReader()
ListBox1.Items.Clear()
Do While Dtrd.Read
ListBox1.Items.Add(New ListItem(Dtrd( "s_bm "), Dtrd( "s_bmid ")))
Loop
Conn.Close()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
dim l as integer
For l = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(l).Selected = True Then
Response.Write( " <script> alert( '选中了 ') </script> ")
End If
Next
Conn.Close()
End Sub
[解决办法]
在pageload里
加上
if not page.ispostback then
...
end if
------解决方案--------------------
Page_Load 中的绑定代码放在 !IsPostBack 中
if(!IsPostBack) {
// ....
}
[解决办法]
Page_Load中的绑定代码放在
If Not Page.IsPostBack Then
End If
之间