Solved

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

Posted on 2015-02-05
1
281 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 47
C# Offline Apllication 5 53
Export import database 4 41
ASP.NET Core index.html  LOCALHOST page cannot be found 2 22
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now