简体   繁体   English

如何从C#中ASP.NET中的背后代码以多选模式检索绑定的列表框?

[英]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.

相关问题 如何处理多选ListBox? ASP.NET C# - How to handle with multiple selection ListBox? asp.net c# c#从asp.net页面访问背后的代码 - c# Code behind access from asp.net page ASP.NET C#从Code Behind设置OnSelectedIndexChanged - ASP.NET C# Set OnSelectedIndexChanged from Code Behind C#ASP.NET从同一mysql命令中选择多个列,并在后面的代码中使用它们 - C# ASP.NET Selecting multiple column from same mysql command and use them in code behind C#ASP.NET使用来自ASP.NET背后代码的布尔变量 - C# ASP.NET using Boolean variable from code behind in ASP.NET 如何从背后的代码绑定ASP.NET Web Forms C#应用程序中下拉列表中的“ SelectedValue”? - How to bind the 'SelectedValue' in dropdown in ASP.NET Web Forms C# application from code behind? 如何在asp.net C#代码后面的菜单控件中禁用特定菜单项 - How to disable specific menu item from menu control in asp.net C# code behind C#ASP.NET如何从Code-Behind插入Eval值重复器 - C# ASP.NET How to insert Eval value Repeater from Code-Behind 如何从Asp.net按钮单击后的C#代码中调用javascript函数? - How to call a javascript function from c# code behind on Asp.net Button click? 如何从ASP.NET,C#后面的代码中使用参数调用javascript函数? - How to Call javascript function with argument from code behind ASP.NET ,C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM