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

Preselecting list items 1

Status
Not open for further replies.

mThomas

Instructor
May 3, 2001
404
0
0
US
I have a web form which has a multiple select dropdown list. The dropdown data is bound from a table. On submission the selected items are pushed to an mssql table.

I have an edit version of the form where the various data is read into the form allowing the user to edit the fields and resubmit the form.

I want the multiple select drowdown to have the currently stored selections highlighted.

I have working code:

Code:
While MyDataReader.Read
Dim goindex As Integer
goindex = MyDataReader("facilityid")
goindex = goindex - 1

facilityidlist.Items(goindex).Selected = True

End While

While the above code works where the goindex corresponds to the correct record. The bound table is sorted by primary key where the primary key sort order is the same as the current index.

As an example in the table facilityid 1 is for Apple, and facilityid 2 if for Pear, facilityid 3 is for Orange and so on.

When I sort the bound table by the facilityid (Primary Key) the list items are sorted Apple, Pear, Orange and so on... Their facilityid is the same as the index of the listbox, so the above code preselects the correct list items.

However...

If the bound table is sorted, say by Alpha, the the sort order is Apple, Orange, Pear and so on... Now the facilityid (Primary Key) does not match the current index of the listobox, so the incorrect list items are selected.

Is there a way to bind or set the index of a listbox to the primary key or index of a table? So if for instance record 4 has been deleted and the sort order is now 1, 2, 3, 5, is it possible to set the index of the listbox to be 1, 2, 3, 5?

I intitialy wanted to loop through the data set and match the text value of the record with the list item text value, using the following code within the datareader.

Code:
facilityidlist.SelectedValue = Convert.ToString(facilityidlist.Items.FindByText(MyDataReader("facilityname")).Value)

That only returns the last list item.

I am using a Windows 2008 server, MS SQL, Visual Studio 2008 and writing in VB.Net.

Any thoughts or suggestions?

tia... mike
 
Code:
mydropdown.Items.FindByValue(value).Selected = true;

Jason Meckley
Programmer

faq855-7190
faq732-7259
 

Works like a charm.

Thank you... mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top