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

Getting Count of Selected Values in ListBox

Status
Not open for further replies.

ietprofessional

Programmer
Apr 1, 2004
267
US
Hi Everyone,

Can someone tell me how to get the count of selected values in ListBox?

Thanks!
 
jet: this may not be what you need; perhaps a one line command may bring back the ct -- in this routine I cycle through the datalist - could easily put a counter in this:

Code:
n = 0
If listContacts.SelectedIndex <> -1 Then          
  If listContacts.SelectionMode = ListSelectionMode.Single Then
  txtCN.Text = listContacts.SelectedItem.Text
  txtCID.Text = listContacts.SelectedItem.Value
Else
  For i = listContacts.SelectedIndex To listContacts.Items.Count - 1
  If listContacts.Items(i).Selected Then
    n = n + 1  
    If n = 1 Then              
      s &= listContacts.Items(i).Text 
      r &= listContacts.Items(i).Value 
    Else       
      s &= ", " & listContacts.Items(i).Text 
      r &= ", " & listContacts.Items(i).Value 
    End If
   End If
  Next
txtCN.Text = s
txtCID.Text = r
End If

...might spur an idea...
 
Code:
int counter =0;
			foreach(ListItem li in ListBox1.Items)
			{
				if(li.Selected)
					counter++;
			}
			Response.Write("The Count Is: " + counter.ToString());
 
Just check if each one is selected e.g.
Code:
        Dim i As Integer = 0
        Dim li As ListItem
        For Each li In ListBox1.Items
            If li.Selected = True Then i += 1
        Next
        Response.Write(i)
Is that what you mean?


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
We must have been writing that at the same time (despite what sedj says about me [lol]). Hey, beats the convertors. Two functional versions for the price of one question.
 
veep, ca:

Wouldn't this be a good candidate to use the CallBack functionality of dot NET?

Paul (link9) and I were discussing this just a couple of threads down.

That is, if you are only going to go for a count, and you want to prevent a post back, seems like this would be a good candidate for implementation of this process.

The links Paul provided are very good; allows complete handling of the event on the clien side...

Just a thought...
 
Veep - I guess sedj was wrong about you and your simultaneous posts (either that or I've started it as well!). [smile]

You're right though it is definately better than any converter!

Isadore - Yeah I saw that thread - very interesting. I guess it's why[b/] ietprofessional needs this count as to whether it would be best to do it client or server side. As you say, if it's just to display how many items a user selected then CallBack may be the way to go but if it's to do any other work on it it may be more suited for server side.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top