Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Getting Multiple selection in ListBox

Reply
Thread Tools

Getting Multiple selection in ListBox

 
 
aroraamit81@gmail.com
Guest
Posts: n/a
 
      02-27-2006
I have a string like "English,Japanese,Spanish" and a list box which
contains say 10 lang.
Now I want my List box should come by selected values as the values in
string and rest of the values to be unselected.

But what I am getting is the last value selected (Spanish in this
case).

Any help.......


Regards,
Amit

 
Reply With Quote
 
 
 
 
S. Justin Gengo [MCP]
Guest
Posts: n/a
 
      02-27-2006
Amit,

Loop through each item and check if each one is selected.

For Each ListBoxItem As ListItem In MyListBox.Items

If ListBoxItem.Selected Then

'---This item is selected do something with it

End If

Next


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
<> wrote in message
news: ups.com...
>I have a string like "English,Japanese,Spanish" and a list box which
> contains say 10 lang.
> Now I want my List box should come by selected values as the values in
> string and rest of the values to be unselected.
>
> But what I am getting is the last value selected (Spanish in this
> case).
>
> Any help.......
>
>
> Regards,
> Amit
>



 
Reply With Quote
 
 
 
 
aroraamit81@gmail.com
Guest
Posts: n/a
 
      02-27-2006
char[] splitter = {','};
string
[]lang=ds.Tables[0].Rows[0]["USER_LANG_ID"].ToString().Split(splitter);

foreach(string i in lang)
{
for(int j=0;j<lstLang.Rows;j++)
{
if(lstLang.Items[j].Text==i)
{
lstLang.SelectedIndex=j;
Response.Write(i);
}
}
}

This is the code I am using... but it does not work....

Pls see.....

 
Reply With Quote
 
S. Justin Gengo [MCP]
Guest
Posts: n/a
 
      02-27-2006
To start with try simplifying the code.

You don't even need to compare the entries with the dataset because the
listbox already has the entiries in it:

for (int j = 0; j < myListBox.Rows; j++)

{

if(myListBox.Items[j].Selected)

{

Response.Write(myListBox.Items[j].Text;

}

}



--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
<> wrote in message
news: oups.com...
> char[] splitter = {','};
> string
> []lang=ds.Tables[0].Rows[0]["USER_LANG_ID"].ToString().Split(splitter);
>
> foreach(string i in lang)
> {
> for(int j=0;j<lstLang.Rows;j++)
> {
> if(lstLang.Items[j].Text==i)
> {
> lstLang.SelectedIndex=j;
> Response.Write(i);
> }
> }
> }
>
> This is the code I am using... but it does not work....
>
> Pls see.....
>



 
Reply With Quote
 
Shunya
Guest
Posts: n/a
 
      02-27-2006
Hi Amit,
Have you changed selection mode from single to multiple??

 
Reply With Quote
 
aroraamit81@gmail.com
Guest
Posts: n/a
 
      02-27-2006
Yeah I have done it......

 
Reply With Quote
 
aroraamit81@gmail.com
Guest
Posts: n/a
 
      02-27-2006
I guess its me who is unable to make u understand.....

Well the databse field tells u the language list (Comma Seprated) which
needs to be selected. So I have to get the values from the databse
itself.....

 
Reply With Quote
 
S. Justin Gengo [MCP]
Guest
Posts: n/a
 
      02-27-2006
Ok, you're trying to select each item in the list based on the dataset. Now
I've got you.

Very similar. First you will certainly have a problem if the text doesn't
match exactly. Strings matching is case sensitive remember. So make certain
that your data and the entries in the list box are exactly the same. Or use
a .ToLower to make certain.

Next your real problem is that you're using SelectedIndex. That keeps
changing the selected index to a single item. It automatically deselects any
other. You have to set each item to selected seperately like this:

foreach(string i in lang)
{
for(int j=0;j<lstLang.Rows;j++)
{
if(lstLang.Items[j].Text==i)
{
lstLang.Items[j].Selected = True;
}
}
}

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
<> wrote in message
news: ups.com...
>I guess its me who is unable to make u understand.....
>
> Well the databse field tells u the language list (Comma Seprated) which
> needs to be selected. So I have to get the values from the databse
> itself.....
>



 
Reply With Quote
 
aroraamit81@gmail.com
Guest
Posts: n/a
 
      02-27-2006
Well Mate I tried it but, did nt work either...........

 
Reply With Quote
 
aroraamit81@gmail.com
Guest
Posts: n/a
 
      02-27-2006
No NO No No No,
IT HAS WORKED...........................................

Thnx a lot Mate.............................................. ...........

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting Multiple Selection Listbox in a TemplateField? Lance Wynn ASP .Net 1 01-30-2008 11:25 PM
updating a SQL database from a multiple selection listbox Ned Balzer ASP .Net 2 08-31-2006 01:28 PM
Multiple Selection in Listbox when Edit the form =?Utf-8?B?V2FyYW4=?= ASP .Net 0 05-04-2006 09:54 PM
Listbox selection to populate another listbox? Chris Kettenbach ASP .Net 3 06-16-2005 09:19 PM
selectedindex of multiple listbox selection in a datagrid =?Utf-8?B?TGll?= ASP .Net 5 12-14-2004 02:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57