0

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.

enter image description here

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>

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;
        }

Any suggestion about how can i set multiple selected value in edit mode from c#?

2
  • I suggest you to use Drop Down CheckBoxList control Commented Jun 28, 2016 at 7:02
  • My dropdown list will have thousand of data, so checkbox is a problem. Select2 suggestion is helpful in that sense. Commented Jun 28, 2016 at 7:09

2 Answers 2

2

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.

EDIT :

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;
        }
Sign up to request clarification or add additional context in comments.

6 Comments

How to set selected list in ListBox from c#?
As you did in your post, your code behind will work with a listBox
Wow, Thanks a lot. As perfect as i wanted.
You cant hit the answer button on my answer to "close" this question :)
Even if your reputation is low you can always mark an answer as the answer in your own question , no ?
|
0

you aren't using select2 in this case ! you need to use select2, and push all you multiple data into a variable and send it to js function or use ajax

$(".ddlAuthor").select2('data', youDataHere);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.