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

Is it possible to display selected items in dropdowncheckboxes text area in asp.net

In my web application, I have dropdowncheckboxes control in that whatever items I selected I need to show that selected items in dropdown textfield as separated with comma.

image description here

I tried this code but I am not getting result:

.aspx: Using this "DropDownCheckBoxes" control

<%@ Register Namespace="Saplin.Controls" Assembly="DropDownCheckBoxes" TagPrefix="asp"%>
<asp:DropDownCheckBoxes ID="ddlInsurance" runat="server"
UseSelectAllNode="true" UseButtons="true"
OnSelectedIndexChanged="ddlInsurance_SelectedIndexChanged"
AutoPostBack="true" CssClass="drpdwnstyle"><Style DropDownBoxBoxWidth="200"/>
</asp:DropDownCheckBoxes>


.CS

protected void dropdown1_SelectedIndexChanged(object sender, EventArgs e)
{
List<String> checkedList = new List<string>();
foreach (ListItem item in dropdown1.Items)
{
if (item.Selected)
{
checkedList.Add(item.Text);
}
dropdown1.DataTextField = String.Join(",", checkedList);
}
}


I tried so much but I am not getting anything.
Can anyone please tell me how to do this.

Answer

Main issue is that DataTextField is not the property you want to set. This property is used together with data source, to let data bound control know which data source's field should be used as text. There is a similar field for value. Correct property to use is Texts.SelectBoxCaption, if I understood your intent correctly.

Also there is no point in assigning this inside foreach, so I moved it out of the loop:

foreach (ListItem item in dropdown1.Items)
{
    if (item.Selected)
    {
        checkedList.Add(item.Text);
    }
}

dropdown1.Texts.SelectBoxCaption = String.Join(",", checkedList);