我有一个列表框,我想从逗号分隔的字符串中选择,但我的代码不起作用。
$sql = "DELETE FROM {$table} WHERE id = ".$_GET['delete_id']." LIMIT 1";
<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>
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;
}
}
答案 0 :(得分:0)
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";