Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox populates textbox with only first record / not selected item

Status
Not open for further replies.

bubberz

Programmer
Dec 23, 2004
117
US
I have a listbox that's populated in the Page_PreRender handler:
Dim strResSQL As String
...code to build strResSQL

Dim cmdRes As New OleDbCommand(strResSQL, Con1)
Con1.Open()
Dim myResRDR As OleDbDataReader = cmdRes.ExecuteReader()
If myResRDR.HasRows = True Then
listResource.DataSource = myResRDR
listResource.DataTextField = "Expr3"
listResource.DataValueField = "GroupCode"
listResource.DataBind()
End Sub

What I want to do is populate a text box right above the list box with the item selected, so I tried the following for the list box:
txtBox.Text = list.SelectedItem.ToString()

The above code only populates the listbox's first item.
I do have "PostBack"=True for the listbox
 
after you select the item...

txtBox.Text = listResource.selectedItem.Text.ToString()

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
checkai,

I changed the handler to:
Private Sub istResource_SelectedIndexChanged..... txtBox.Text = list.SelectedItem.Text.ToString()
End Sub

...and same thing. The textbox populates with the first record each time.
 
where are you setting which item in the drop down is selected?

if you want it to be on selectedindexchanged then the drop down would need to be AutoPostBack=True...

You're quite busy in these forums the last few days!!

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
checkai,

Yeah, I can't even get to sleep at night as I can't stop thinking about this page with these three ddls.
For this third ddl, yes, I have it as AutoPostBack = True.
I populate it on Page_PreRender
I don't understand why it's taking the first record each time, and not the item I've selected.
 
i've never done things on Prerender before...is that encouraged?

where do you select an item? and where do you set the textbox text?

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
First I select from two dropdownlists. Both are set to AutoPostBack=True. These two build an SQL query to populate the listbox. This third list box is built in Page_PreRender, and I try to populate the textbox when the Private Sub istResource_SelectedIndexChanged is fired.
 
is this the 2nd list? istResource_SelectedIndexChanged ?

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
can you post your code for that SelectedIndexChanged sub? I'm wondering if it's because of the binding in the PreRender...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Private Sub listResource_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listResource.SelectedIndexChanged
txtResource.Text =
listResource.SelectedItem.Text.ToString()
End Sub
 
what i think is happening, is that, when you do the selectedindex change, you're actually rebinding the dropdownlist and only the first item is selected...

i would move the binding of the 3rd ddlist out of prerender

to test, you could do this

in prerender
response.write("prerender")

in selectedindexchange
response.write("changing")

i'm assuming that the "prerender"'s will be before the "changing"

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
checkai,
You're right....it only populates with "Prerender" and never populates with "Changing"
I tried to move the loading of the third ddl around to the SelectedIndexItemChanged of the 2nd DDL, and that didn't work, and tried to put it in the Else of the Page_Load handler where there was a postback, but that didn't work. I'd get an error about the connection already being open for one of my DataReaders.
 
oh boy...do you have public variables that you reuse?

hmmm...do you populate other things in PreRender's?

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
No public variables.
I populate the 2nd ddl in the PreRender
 
I think that's the issue...as well...is there a reason you're using prerender..?

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Not really.......
Where else should I be populating the 2nd ddl?
Or should I say, what's another option?
 
I have never used prerender subs...everything i do is within the Page_Load or if things are based off of drop downs, then a button or the selectedIndexChange postback...

dlc

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
checkai,
I removed/commented out everything in the Page_PreRender handler.
I build and bind the 2nd ddl on the 1st ddl's SelectedIndexItemChanged.

I build and bind the 3rd listbox on the 2nd ddl's SelectedIndexItemChanged

The same thing is happening when I select the third list where the first record is always inserted into the textbox right above.
 
and the 3rd list box's selected change still looks like this?

Code:
Private Sub listResource_SelectedIndexChanged(ByVal sender   As Object, ByVal e As System.EventArgs) Handles   listResource.SelectedIndexChanged
txtResource.Text =  
listResource.SelectedItem.Text.ToString()
End Sub

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top