Solved

asp.net C# best practices for setting selected items in listbox

Posted on 2015-02-05
1
291 Views
Last Modified: 2015-03-04
experts,

I'm currently setting the selected items of listitems in my list box from a sql database using this loop:

 while (rdr.Read())
{
    foreach (ListItem item in myList.Items)
    {
        if (rdr["CODE"].ToString() == item.Value.ToString())
            item.Selected = true;
    }
}

I'm pretty sure this isn't the most efficient method. Can anyone give me an example of the best way to set my selected items in a listbox from a dataset.

Thanks in advance
0
Comment
Question by:telliot79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 17

Accepted Solution

by:
OriNetworks earned 500 total points
ID: 40591271
For this purpose I created a reusable utility function that works like this:
This is not actual code, only pseudo code
function SetSelectedByValue(DropDownList ddl, String valueToSelect, String textOfItem(optional), Boolean InsertIfNotExists )
{
//make sure any currently selected item is unselected
if ddl.SelectedItem is nothing Then
{
ddl.SelectedItem.selected = false
}

if (ddl.Items.FindByValue(LookupValue) !=null) 
{
//if found then select it
ddl.Items.FindByValue(LookupValue).Selected = True
}
else
{
if (nsertIfNotExists=true)
{
//insert item
ddl.Insert(0, new ListItem(textOfItem,valueToSelect))

}
}

Open in new window

0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question