Listbox in asp.net not getting selected items

I have multiple dropdown & listbox in my webpage.

I am trying to get a list of CategoryID from a lstCatID listbox i am able to populate the listbox with category name.

If i remember correctly in first attempt my code worked fine, after that i made some change then it stated to always get the first item selected x No. of time

<asp:ListBox ID="lstCatID" runat="server" DataTextField="CategoryName" 
                DataValueField="CategoryID" SelectionMode="Multiple" CssClass="lstListBox">
 </asp:ListBox>



protected void Button1_Click(object sender, EventArgs e)
{
    string CatID = string.Empty;
    foreach (ListItem li in lstCatID.Items)
    {
        if (li.Selected == true)
        {
           // Response.Write();
            CatID += lstCatID.SelectedItem.Value + ",";
        }
    }
    Response.Write(CatID);
}

I am not sure what is going wrong i checkd MSDN it show exactly the same way of doing it.

May be i am doing something wrong.

Just to add using firefox i am able to see multiple selected value have selected property.

<option value="3" selected="selected">One</option>
<option value="2">Two</option>
<option value="29" selected="selected">Three</option>
<option value="25" selected="selected">Four</option>
<option value="22" >Five</option>

My output in this case will be 3,3,3

I would appreciate help in this regard

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I am not sure what is wrong with the logic i am using.

I came across a nice solution using LINQ.

This single statement works great & gets me the desired results.

string values = String.Join(", ", lstCatID.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Value).ToArray());

RESULT: 3,29,25

Method 2

You are setting it to the same value every time:

foreach (ListItem li in lstCatID.Items)
{
    if (li.Selected == true)
    {
       // you are always using lstCatID.SelectedItem.Value.
        CatID += lstCatID.SelectedItem.Value + ",";
    }
}

When you actually want the value of the item in your loop that is selected:

foreach (ListItem li in lstCatID.Items)
{
    if (li.Selected == true)
    {
        // get the value of the item in your loop
        CatID += li.Value + ",";
    }
}

Method 3

Try to add Page.IsPostback on your Page_Load like

protected void Page_Load(object sender, EventArgs e)
{
    // Do your API code here unless you want it to occur only the first
    // time the page loads, in which case put it in the IF statement below.
    if (!Page.IsPostBack)
    {

    }
}

Code:

protected void Button1_Click(object sender, EventArgs e)
{
    string CatID = string.Empty;
    foreach (ListItem li in lstCatID.Items)
    {
        if (li.Selected )
        {
           // TODO: Whatever you are doing with a selected item.
        }
    }
    Response.Write(CatID);
}

Once i was facing the same problem and i made Postback mistake.

Hope it works.

Method 4

Get the selected items using linq

var selected = lstCatID.Items.Where(i => i.Selected);

Method 5

Minutes later I found a solution:

If lstLocations.Items.Count > 0 Then
            For i As Integer = 0 To lstLocations.Items.Count - 1
                If lstLocations.Items(i).Selected Then
                    'insert command
                    Dim selectedItem As String = lstLocations.Items(i).Text
                End If
            Next
        End If

This worked fine in my scenario


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x