here is bit of code...
My ListBox
<asp:listbox ID="txt_listahan" OnSelectedIndexChanged="Change1" AutoPostBack="true" Width="233px" Rows="20" runat="server" SelectionMode="single"></asp:listbox
My Query
sql = "SELECT tblJobs.JName as ID1, tblJobs.JobID as ID3 FROM tblJobs"
Dim strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("...")
Dim objConn as New OleDBConnection(strConnString)
Dim ds as Dataset = New DataSet()
Dim objAdapter as New OleDbDataAdapter(sql,objConn)
Dim objCmdBld as New OleDbCommandBuilder(objAdapter)
objAdapter.Fill(ds,"List")
txt_listahan.DataSource = ds.Tables("List").DefaultView
txt_listahan.DataBind()
objConn.Close()
My Change1()
Sub Change1(sender As Object, e As System.EventArgs)
' session("jobname") = txt_listahan.SelectedValue
try
txt_time.text = txt_listahan.SelectedItem.Text
catch
txt_time.text = txt_listahan.SelectedIndex
end try
End SubMost likely the problem is in the Page_Load. I guess you're just doing the databinding over there? What you should do instead is this:
If Not Page.IsPostBack Then
'do the binding
End If
The reason for this is that the Page_Load is always executing (also on a post back caused by one of the controls) and thus that by doing the databinding again, the selection is lost...
yeah you're right...it works, dang you guys are good...
but next question, now it works well for the first run...what if i want to populate the listbox from a query result? how do i bind that data? or is this not advisable?
I finally found a site that help me populate the list box from a database.
http://www.vb-helper.com/howto_net_db_load_listbox.html
I took out my datavaluefield and datatextfield from my asp:listbox control...and bam it works...
So when you kept DataValueField and DataTextField in the control tag, it didn't work?
I'm curious also as to why that example uses a loop when there's already a DataBind method built in to the control...
when i kept them in the tag, my values didn't show. Cause i think what is happening; is it still searches for that fieldname. I used databind at first but whenever i rebind the listbox from a query, I couldn't keep track of the value selected. My index always remained at -1. So i used this solution because my index change along with it :)
0 comments:
Post a Comment