Shiv Shiv - 1 year ago 123
ASP.NET (C#) Question

How can i show the selected items as comma separated in the Multi select dropdown text field in

In my web application, i am trying to do show the selected items as comma separated in the Multi select drop down text field is it possible to do and how can i do this can any one tell me. sample image is link [As Shown in figure]

I tried:


<asp:DropDownCheckBoxes ID="dropdown1" runat="server" UseSelectAllNode="true" UseButtons="true" OnSelectedIndexChanged="dropdown1_SelectedIndexChanged" AutoPostBack="true"><Style DropDownBoxBoxWidth="200"/></asp:DropDownCheckBoxes>


public void ddlsbind()
string str=string.Empty;
string query = "select distinct col1 from Collection where sample='" + ddl2.SelectedItem.Text.ToString() + "' and col1 IS NOT NULL";
SqlCommand cmd = new SqlCommand(query, con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
ddlInsurance.DataSource = dt;
ddlInsurance.DataTextField = "col1";
ddlInsurance.DataValueField = "col1";

Can anyone help me to how to do this.

Thank you

Answer Source


private void dropdown1_SelectedIndexChanged(object sender, EventArgs e){
    List<string> checkedList = new List<string>();
    foreach(checkbox c in dropdown1){
    myTextField.Text = String.Join(",",checkedList);

The code you're looking for is specifically set forth in the documentation for your control:

  protected void btnSubmit_Click(object sender, EventArgs e)

    List<String> CountryID_list = new List<string>();
    List<String> CountryName_list = new List<string>();
    foreach (System.Web.UI.WebControls.ListItem item in ddchkCountry.Items)
        if (item.Selected)
       lblCountryID.Text = "Country ID: "+ String.Join(",", CountryID_list.ToArray());
       lblCountryName.Text = "Country Name: "+ String.Join(",", CountryName_list.ToArray());


In your specific case, since text and value are the same, you only need one list:

   protected void dropdown1_SelectedIndexChanged(object sender, EventArgs e)
     List<String> checkedList = new List<string>();
     //List<String> CountryName_list = new List<string>();
     foreach (System.Web.UI.WebControls.ListItem item in dropdown1.Items)
        if (item.Selected)
        //I don't know the ID/name of your textbox. You need to change that here:
         myTextBox.Text = String.Join(",", checkedList);
         //lblCountryName.Text = "Country Name: "+ String.Join(",", CountryName_list.ToArray());

Change the textbox from MyTextBox to the actual name of the textbox you are using and that should do it. Take a look at how I modified the sample code and use that to modify your own from here on out. Look at what it's doing.

Additional Information remember that the control you are using is custom, so it will have properties/methods/settings not fully available through VS intellisense in the manner that standard controls are. You'll have to visit to see how to manage your custom control.

Finally, I'd suggest staying away from custom controls until you understand a little bit more about coding .Net Web Applications - stick to standard controls with well-documented features/parameters that you can easily look up from multiple sources. Venture into custom controls (or create your own - they're easy) after you've mastered the basics.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download