Shiv Shiv - 2 months ago 9
ASP.NET (C#) Question

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

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:

.aspx:

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


.CS

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);
da.Fill(dt);
ddlInsurance.DataSource = dt;
ddlInsurance.DataTextField = "col1";
ddlInsurance.DataValueField = "col1";
ddlInsurance.DataBind();
}


Can anyone help me to how to do this.

Thank you

Answer

psuedoCode:

private void dropdown1_SelectedIndexChanged(object sender, EventArgs e){
    List<string> checkedList = new List<string>();
    foreach(checkbox c in dropdown1){
        if(c.Checked){
            checkedList.Add(c.Value/Text/Whatever);
        }
    }
    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)
       {
           CountryID_list.Add(item.Value);
           CountryName_list.Add(item.Text);
       }
       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)
        {
            //CountryID_list.Add(item.Value);
            checkedList.Add(item.Text);
        }
        //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 http://saplin.blogspot.com/2011/05/dropdowncheckboxes-using-control.html 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.