Getting Multiple selection in ListBox

Discussion in 'ASP .Net' started by aroraamit81@gmail.com, Feb 27, 2006.

  1. Guest

    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
    , Feb 27, 2006
    #1
    1. Advertising

  2. 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:...
    >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
    >
    S. Justin Gengo [MCP], Feb 27, 2006
    #2
    1. Advertising

  3. Guest

    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.....
    , Feb 27, 2006
    #3
  4. 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:...
    > 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.....
    >
    S. Justin Gengo [MCP], Feb 27, 2006
    #4
  5. Shunya Guest

    Hi Amit,
    Have you changed selection mode from single to multiple??
    Shunya, Feb 27, 2006
    #5
  6. Guest

    Yeah I have done it......
    , Feb 27, 2006
    #6
  7. Guest

    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.....
    , Feb 27, 2006
    #7
  8. 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:...
    >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.....
    >
    S. Justin Gengo [MCP], Feb 27, 2006
    #8
  9. Guest

    Well Mate I tried it but, did nt work either...........
    , Feb 27, 2006
    #9
  10. Guest

    No NO No No No,
    IT HAS WORKED...........................................

    Thnx a lot Mate.........................................................
    , Feb 27, 2006
    #10
  11. Oh, good!

    I was just about to email you a working example based on the previous code.


    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    <> wrote in message
    news:...
    > No NO No No No,
    > IT HAS WORKED...........................................
    >
    > Thnx a lot Mate.........................................................
    >
    S. Justin Gengo [MCP], Feb 27, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TGll?=
    Replies:
    5
    Views:
    673
    =?Utf-8?B?TGll?=
    Dec 14, 2004
  2. Chris Kettenbach

    Listbox selection to populate another listbox?

    Chris Kettenbach, Jun 15, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,636
    Chris Kettenbach
    Jun 16, 2005
  3. =?Utf-8?B?V2FyYW4=?=

    Multiple Selection in Listbox when Edit the form

    =?Utf-8?B?V2FyYW4=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    476
    =?Utf-8?B?V2FyYW4=?=
    May 4, 2006
  4. Ned Balzer
    Replies:
    2
    Views:
    706
    Ned Balzer
    Aug 31, 2006
  5. Lance Wynn
    Replies:
    1
    Views:
    843
    Angel
    Jan 30, 2008
Loading...

Share This Page