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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmtically setting multiple list items to SELECTED 2

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
US
We are using asp.net 2.0

We have a list box that we load with values in the page load and also try to set one or more values in the same page load event. It appears that the list box has zero items in the items collection at the time we try to select the items we want to highlight. Even though after the page is loaded we see the list box with all its choices.

Does not the list box get rendered until after the page loads?

How would we do this?

Even in the Load Complete event we seem to show (with a msgbox) zero items in the items collection when we add them at the first of the page load.

A little guidance would be helpful. Thanks.

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
How are you loading the list box? Are you binding or manually loading it with values?
 
I work with Lonnie, the gentleman who started this thread and asked our question.

Thanks for your replies!

Thanks because it works now that we put in the line of code to bind.

We have in the page load event the following in the following order:

some code that gets session variable because the sql data source for the listbox uses the variable to give the user some reasons from which to choose based upon the user's department and then ...
Me.listBoxReasonForTransport.DataSourceID = "SqlDataSourceReasons"

When we add

Me.listBoxReasonForTransport.DataBind()

we can now select the reasons that were chosen when the request was saved by the user as we now are in edit mode (using the Add page also as the Edit page)

there is code that selects the reasons using a datareader and then:

Do Until dtrReasons.Read() = False
Dim valueOfReason As Integer
valueOfReason = dtrReasons("transportationReasonId")
For Each item In listBoxReasonForTransport.Items
If item.Value = valueOfReason Then
item.Selected = True
End If
Next

I originally failed to have the line of code to bind the listbox of reason and therefore the count was zero when trying to select/highlight the reasons that were chosen when the request was saved.

It works now that we are databinding as you suggested.

Thanks again!
 
Thanks guys, you both are always there when we need ya.

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top