C# asp.net 中的列表框未获取所选项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16338246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:47:47  来源:igfitidea点击:

Listbox in asp.net not getting selected items

c#asp.netlistbox

提问by Learning

I have multiple dropdown & listbox in my webpage.

我的网页中有多个下拉列表和列表框。

I am trying to get a list of CategoryIDfrom a lstCatIDlistbox i am able to populate the listbox with category name.

我正在尝试CategoryIDlstCatID列表框中获取一个列表,我可以使用类别名称填充列表框。

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

如果我在第一次尝试时没记错的话,我的代码运行良好,之后我做了一些更改,然后它声明始终选择第一个项目 x 时间

<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.

我不确定出了什么问题,我检查了 MSDN,它显示的方法完全相同。

May be i am doing something wrong.

可能是我做错了什么。

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

只需使用 Firefox 添加,我就可以看到多个选定的值具有选定的属性。

<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

在这种情况下我的输出将是 3,3,3

I would appreciate help in this regard

我将不胜感激这方面的帮助

采纳答案by MikeSmithDev

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

回答by Rahul

Try to add Page.IsPostbackon your Page_Load like

尝试在您的 Page_Load 上添加Page.IsPostback

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.

希望它有效。

回答by Learning

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

我不确定我使用的逻辑有什么问题。

I came across a nice solution using LINQ.

我使用 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

结果:3,29,25

回答by waris kantroo

Get the selected items using linq

使用 linq 获取所选项目

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

回答by user2596200

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

这在我的场景中运行良好