Listbox in asp.net not getting selected items

前端 未结 5 1064
花落未央
花落未央 2021-01-14 10:41

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

5条回答
  •  不知归路
    2021-01-14 11:33

    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 + ",";
        }
    }
    

提交回复
热议问题