1

I am trying to get the value at selected indicies in a ListBox using ASP.net C#

MakeListBox.SelectionMode = ListSelectionMode.Multiple;
int [] indicies= MakeListBox.GetSelectedIndices();

I am going to dynamically building a select statement for a database query to an SQLDataSource. What I had hoped to be able to do was get all the selected indexes go through a loop to for the array that it returns and add each value at the specified index to a string.

I have looked through this and this but can't find what is needed to do what I have talked about.

Basically I am looking for the opposite of the IndexOf command. Or a technique that would have the same results.

1
  • System.Windows.Forms != ASP.Net Commented Jun 25, 2013 at 20:22

1 Answer 1

2

It's not clearly obvious when looking at the ListBox control properties, but this is the best way to do it:

foreach(ListItem li in MakeListBox.Items)
{
   if(li.Selected)
      {
         // Append to your string list
      }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That is wonderful. Thank you. Yeah, as Java programmer having to switch over to C# for my job, the foreach loop was a bit new to me. Though Delegates still confuse me a bit. Thanks again. I would upvote you if I could.
And SLaks' comment is important to keep in mind as you are learning C#. The WinForms controls and ASP.NET controls may share names, but they can be very different in practice. Whenever looking at MSDN, make sure you are looking at the correct article (WinForms vs ASP.NET, .NET version, etc.) Good luck in your transition!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.