在C#中的asp.net下拉列表中的select2上设置多个选定值 [英] set multiple selected value on select2 multiple in asp.net dropdownlist from C#
问题描述
我需要在一本书下添加多位作者.因此,我在asp dropdownlist中使用了"Select2 Multiple".我能够插入数据,但是在编辑模式下我无法在下拉列表中分配多个选定的值.
I need to add multiple author under a book. So i have used "Select2 Multiple" in asp dropdownlist. I was able to insert data but in edit mode i can't assign multiple selected value on dropdownlist.
我在aspx页面中的下拉列表是
My dropdownlist in aspx page is
<asp:DropDownList ID="ddlAuthor" runat="server" multiple="multiple" data-placeholder="Select Author (s)" CssClass="form-control select2"></asp:DropDownList>
在C#中,我尝试了以下操作,但它仅添加了一个值.我的C#代码是
In C# i have tried as following but it add only one value. My c# code is
foreach (DataRow row in dtAuthor.Rows)
{
ddlAuthor.Items.FindByValue(row["AUTHOR_ID"].ToString()).Selected = true;
}
关于如何在C#中以编辑模式设置多个选定值的任何建议?
Any suggestion about how can i set multiple selected value in edit mode from c#?
推荐答案
您不能使用DropDownList实现此目的,而应使用像这样的listBox:
You can't use a DropDownList to achieve this, use a listBox like this :
<asp:ListBox ID="ddlAuthor" runat="server" SelectionMode="Multiple" data-placeholder="Select Author (s)" CssClass="form-control select2"></asp:ListBox>
您后面的代码将起作用.
And your code behind will work.
以下是为您提供的代码示例:
Here is a example of code behind for you :
//I bind my ListBox with random data
List<string> data = new List<string>() { "Flo", "Auteur", "Patrick","Test" };
//Databind
ddlAuthor.DataSource = data;
ddlAuthor.DataBind();
//Here is my selected values, i wish that this values are selected
List<string> selected = new List<string>() { "Flo", "Patrick" };
//Foreach value in my selected list i select the proper value in my listbox
foreach (string row in selected)
{
ddlAuthor.Items.FindByValue(row).Selected = true;
}
这篇关于在C#中的asp.net下拉列表中的select2上设置多个选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!