简体   繁体   English

在Asp.net中填充逗号分隔的字符串以多选ListBox-C#

[英]Populate comma separated string to multi select ListBox in Asp.net - C#

I have a List Box which i want to get selected from a comma separated string but my code is not working. 我有一个列表框,我想从逗号分隔的字符串中选择它,但是我的代码不起作用。

ASPX: ASPX:

<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
    <asp:ListItem Value="1">aaa</asp:ListItem>
    <asp:ListItem Value="2">bbb</asp:ListItem>
    <asp:ListItem Value="3">ccc</asp:ListItem>
    <asp:ListItem Value="4">ddd</asp:ListItem>
    <asp:ListItem Value="5">eee</asp:ListItem>
    <asp:ListItem Value="6">fff</asp:ListItem>
</asp:ListBox>

ASPX.CS: (Code) ASPX.CS :(代码)

string listboxvalues = "2,1,5";
for (int i = 0; i < ListBox1.Items.Count; i++)
{
    foreach (string category in listboxvalues.ToString().Split(','))
    {
        if (category != ListBox1.Items[i].Value) continue;
            ListBox1.Items[i].Selected = true;
            break;
    }
}

Expected Result: (The expected result is what i am expecting but the code is not selecting anything) 预期结果:(预期结果是我所期望的,但是代码未选择任何内容)

<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
    <asp:ListItem Value="1" Selected="True">aaa</asp:ListItem>
    <asp:ListItem Value="2" Selected="True">bbb</asp:ListItem>
    <asp:ListItem Value="3">ccc</asp:ListItem>
    <asp:ListItem Value="4">ddd</asp:ListItem>
    <asp:ListItem Value="5" Selected="True">eee</asp:ListItem>
    <asp:ListItem Value="6">fff</asp:ListItem>
</asp:ListBox>

ASPX.CS: (Code) ASPX.CS :(代码)

string listboxvalues = "2,1,5";

//declare a list

List<string> items = new List<string>()

for (int i = 0; i < ListBox1.Items.Count; i++)
{
    foreach (string category in listboxvalues.ToString().Split(','))
    {
        if (category != ListBox1.Items[i].Value) continue;
            items.Add(category);
            break;
    }
}

//then in your ListBox1

listBox1.DataSource = items;
        listBox1.DisplayMember = "Item";
        listBox1.ValueMember = "Value";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net的“多选”下拉文本框中以逗号分隔的形式显示所选项目 - How can i show the selected items as comma separated in the Multi select dropdown text field in asp.net 如果使用c#在asp.net中包含字符串,则选择列表框项 - Select listbox item if contain a string in asp.net using c# 使用ASP.NET和C#在ListBox中选择多个值 - Select multiple value in ListBox using ASP.NET and C# 如何用字符串分隔的逗号填充 C# 列表框 - How to populated C# listbox with string separated comma ASP.NET / C#-如何弹出带有列表框/多选控件的页面并将选定的ID发送回父页面 - ASP.NET / C# - How to pop up a page with a listbox / multi-select control and send the selected IDs back to the parent page ASP.NET如何选择用逗号分隔的字符串的复选框列表项 - ASP.NET how to select checkbox list items with comma separated string 如何使用 c# asp.net 从具有逗号分隔值的列的表中获取记录? - How to fetch records from table having column with comma separated values using c# asp.net? 如何在C#linq asp.net MVC中使用逗号分隔值列连接两个表 - how to join two tables with comma separated values column in c# linq asp.net mvc ASP.Net中的C#列表框 - c# listbox In ASP.Net ListBox中的DateTime C#Asp.Net - DateTime in ListBox C# Asp.Net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM