[英]How to retrieve binded listbox in multiple selection mode from code behind in ASP.NET in C#?
I have chosen listbox . 我选择了列表框 。 I have no problem to add items to the database from this listbox, but when I want to edit my post and retrieve these Tags from the database to this listbox, I cant add selected items from the database.
我可以从此列表框中向数据库添加项目没有问题,但是当我要编辑帖子并将这些标签从数据库检索到此列表框中时,我无法从数据库中添加选定的项目。
I need to retrieve and add selected Tags by user to this list box since the user needs to look at his selected Tags. 我需要按用户检索所选标签并将其添加到此列表框中,因为用户需要查看其所选标签。
My code is below: 我的代码如下:
<asp:SqlDataSource ID="sqldsSkillsNeed" runat="server" ConnectionString='<%$ ConnectionStrings:NetProjectsConnStr %>' SelectCommand="SELECT * FROM [tblAutoTags]"></asp:SqlDataSource>
<asp:ListBox ID="lstSkills" data-placeholder="your skills" SelectionMode="Multiple" CssClass="form-control chosen-select chosen-rtl" TabIndex="8" runat="server" DataSourceID="sqldsSkillsNeed" DataTextField="TagTitle" DataValueField="id"></asp:ListBox>
and code behind like this 像这样的背后代码
string str = dt.Rows[0]["skills"].ToString();
string[] values = str.Split(',');
foreach (string value in values)
{
if (value.Trim() == "")
continue;
lstSkills.Items.Add(value);
}
This code works correctly, but did not retrieve the selected Tags in list box in this line: 这段代码可以正常工作,但是没有在此行的列表框中检索选定的标签:
lstSkills.Items.Add(value);
Just try using FindByValue property of Listview as follow... 只需尝试使用Listview的FindByValue属性,如下所示...
foreach (string str in selectedStr)
{
if(lstSkills.Items.FindByValue(str) != null)
{
lstSkills.Items.FindByValue(str).Selected = true;
}
}
try this 尝试这个
string[] values = str.Split(',');
lstSkills.Items.AddRange(values);
ty my problem resolved, but now i need to delete selected item from listbox but still keep selected item? ty我的问题已解决,但现在我需要从列表框中删除选定的项目,但仍保留选定的项目? look this picture: ListBox Image in this image you can see selected items but these items is double and i need to delete selected items from list
看这张图片:在这张图片中的列表框图像 ,您可以看到选定的项目,但是这些项目是双重的,我需要从列表中删除选定的项目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.