listbox question

J

Jon

Is there a way to populate a listbox and highlight the items passed to it
from a prior page?

I have page1 that has a grid view with 2 columns (customers, locations) I
need to be able to select a row (customer) and click update, then go to my
update page and carry over the customer name and locations. The locations
(all locations) have to populate a listbox but I need to selected only the
locations that are showing in the grid on the previous page. Is there a way
to do this?
 
D

David R. Longnecker

Jon-

Since I don't have "data" to work with, here's a quick example using a Dictionary
generic list (it's got two "columns", so it should work as an example for
your data source).

// Emulate an incoming, two-column datasource.
// If you don't have this information, there's nothing to say you
// can't collect it when moving to the Update Page and storing in
Session.
Dictionary<string, string> quickList = new Dictionary<string, string>();
quickList.Add("Customer #1", "New York");
quickList.Add("Customer #2", "Austin");
quickList.Add("Customer #3", "New York");
quickList.Add("Customer #4", "Wichita");
quickList.Add("Customer #5", "St. Louis");


// Populate our listbox with Cities, assuming this is
// populated from another datasource, so just imagine.
HighlightedListBox.Items.Add("Boston");
HighlightedListBox.Items.Add("New York");
HighlightedListBox.Items.Add("Redmond");
HighlightedListBox.Items.Add("Dallas");
HighlightedListBox.Items.Add("Austin");
HighlightedListBox.Items.Add("Kansas City");
HighlightedListBox.Items.Add("Wichita");
HighlightedListBox.Items.Add("St. Louis");


// Now, loop through the ListBox object
// and "select" those that appear in our quicklist
// datasource.

foreach (ListItem item in HighlightedListBox.Items)
{
if (quickList.ContainsValue(item.Value))
{
item.Selected = true;
}
}

In your ListBox object on the ASPX page, make sure to have SelectionMode=Multiple
set.

HTH.

-dl
 
J

Jon

there is no .ContainsValue() on the listbox.


David R. Longnecker said:
Jon-

Since I don't have "data" to work with, here's a quick example using a
Dictionary generic list (it's got two "columns", so it should work as an
example for your data source).

// Emulate an incoming, two-column datasource.
// If you don't have this information, there's nothing to say you
// can't collect it when moving to the Update Page and storing in
Session.
Dictionary<string, string> quickList = new Dictionary<string,
string>();
quickList.Add("Customer #1", "New York");
quickList.Add("Customer #2", "Austin");
quickList.Add("Customer #3", "New York");
quickList.Add("Customer #4", "Wichita");
quickList.Add("Customer #5", "St. Louis");

// Populate our listbox with Cities, assuming this is
// populated from another datasource, so just imagine.
HighlightedListBox.Items.Add("Boston");
HighlightedListBox.Items.Add("New York");
HighlightedListBox.Items.Add("Redmond");
HighlightedListBox.Items.Add("Dallas");
HighlightedListBox.Items.Add("Austin");
HighlightedListBox.Items.Add("Kansas City");
HighlightedListBox.Items.Add("Wichita");
HighlightedListBox.Items.Add("St. Louis");


// Now, loop through the ListBox object
// and "select" those that appear in our quicklist // datasource.

foreach (ListItem item in HighlightedListBox.Items)
{
if (quickList.ContainsValue(item.Value))
{
item.Selected = true;
}
}

In your ListBox object on the ASPX page, make sure to have
SelectionMode=Multiple set.

HTH.

-dl
 
D

David R. Longnecker

Correct, the quicklist object is a Dictionary object, used to emulate a datasource.

My assumption is that you know of the items you're passing page-to-page and
can encapsulate that into an object (either a generic collection, object,
or simply a DataTable) and iterate through it.

On Page2 (the page you are updating on), you state you have to populate a
listbox, but only need the selected to highlight from the prior page. I
made the assumption that you have a clean datasource providing that; the
missing link was something to pick out the selections.

Is it possible you could post up the flow of your code? That would help
clear up your usage. :)

Thanks!

-dl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

select listbox 1
listbox 0
blanking a listbox 1
Filter table rows based on multiple checkboxes value 2
listbox woes 2
Listbox 1
Formatted Listbox? 1
array to listbox 18

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,235
Latest member
Top Crypto Podcasts_

Latest Threads

Top