[英]Select multiple value in ListBox using ASP.NET and C#
Why "SelectedValue" selects only the first value in my multiple selection mode ? 为什么“ SelectedValue”在我的多重选择模式下仅选择第一个值?
<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"; <-- only this is selects ListTag.SelectedValue = "Tag2";
in your asp page you have this. 在您的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>
then in your cs(Code Behind) in the event that you want you have this. 然后在您想要的情况下,在您的CS(代码隐藏)中。
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;
}
}
that will result in Tags 1,3,6,9 selectd 这将导致选择标签1,3,6,9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.