C# 使用ASP.NET和C在列表框中选择多个值#
为什么“SelectedValue”在我的多选模式中只选择第一个值C# 使用ASP.NET和C在列表框中选择多个值#,c#,asp.net,C#,Asp.net,为什么“SelectedValue”在我的多选模式中只选择第一个值 <dt>Tags:</dt><dd> <asp:ListBox ID="ListTag" runat="server" SelectionMode="Multiple" DataSourceID="SqlDataSourceTag" DataTextField="tag_name" DataValueField="tag_id"> </asp:ListBox
<dt>Tags:</dt><dd>
<asp:ListBox ID="ListTag" runat="server" SelectionMode="Multiple"
DataSourceID="SqlDataSourceTag" DataTextField="tag_name"
DataValueField="tag_id">
</asp:ListBox>
<asp:SqlDataSource ID="SqlDataSourceTag" runat="server"
ConnectionString="<%$ ConnectionStrings:db_cc %>">
</asp:SqlDataSource>
</dd>
标签:
ListTag.SelectedValue=“Tag1” 在你的asp页面中,你有这个
<asp:ListBox ID="ListTag" runat="server" AutoPostBack="True" SelectionMode="Multiple" Width="250px">
<asp:ListItem Selected="False" Text="Tag-1" Value="1">Tag - 1</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-2" Value="2">Tag - 2</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-3" Value="3">Tag - 3</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-4" Value="4">Tag - 4</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-5" Value="5">Tag - 5</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-6" Value="6">Tag - 6</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-7" Value="7">Tag - 7</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-8" Value="8">Tag - 8</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-9" Value="9">Tag - 9</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-10" Value="10">Tag - 10</asp:ListItem>
</asp:ListBox>
for (int i = 0; i < lsBox.Items.Count; i++)
{
var item = lsBox.Items[i];
string[] selectecvalues = new string[] { "1", "3", "6", "9" };
if (selectecvalues.Contains(item.Value)) // or item.Text if you like
{
lsBox.Items[i].Selected = true;
}
}
标签-1
标签-2
标签-3
标签-4
标签-5
标签-6
标签-7
标签-8
标签-9
标签-10
然后,在您的cs(代码隐藏)中,如果您需要,您将拥有此功能
<asp:ListBox ID="ListTag" runat="server" AutoPostBack="True" SelectionMode="Multiple" Width="250px">
<asp:ListItem Selected="False" Text="Tag-1" Value="1">Tag - 1</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-2" Value="2">Tag - 2</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-3" Value="3">Tag - 3</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-4" Value="4">Tag - 4</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-5" Value="5">Tag - 5</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-6" Value="6">Tag - 6</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-7" Value="7">Tag - 7</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-8" Value="8">Tag - 8</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-9" Value="9">Tag - 9</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-10" Value="10">Tag - 10</asp:ListItem>
</asp:ListBox>
for (int i = 0; i < lsBox.Items.Count; i++)
{
var item = lsBox.Items[i];
string[] selectecvalues = new string[] { "1", "3", "6", "9" };
if (selectecvalues.Contains(item.Value)) // or item.Text if you like
{
lsBox.Items[i].Selected = true;
}
}
for(int i=0;i
这将导致标记1,3,6,9 selectdSelectedValue
只表示单个值,这就是为什么您会得到第一个选定值。如果你想得到所有的选择值,你需要遍历列表中的项目,检查每个项目的selected属性,如果它是真的,就得到它的值。它不起作用。我必须以编程方式选择几个标记。无需检查是否选择了某些选项。我没有ListTag中的方法,SetSelected“”。我有ListTag.Items.Count=0,因此我无法为此循环,但我有多个选项标记choices@Daniel您需要在之前加载数据,称为loopingtanks@ThaNet I我忘记添加ListTag.DataBind();:)