This is a migrated thread and some comments may be shown as answers.

Radcombox with checkboxes selection

1 Answer 43 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Mani PC
Top achievements
Rank 1
Mani PC asked on 03 Apr 2013, 04:20 PM

Hello,
I have combobox1 populated with data have multiple selection checkboxes in it…
I have another combobox2 which will get populated based on checked items in the combobox1…

I want to know how to do this from codebehind…

what events we need to use and sample code will help....

Thanks

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 04 Apr 2013, 10:33 AM
Hi Mani,

Please try the following sample code snippet I tried at my end.

ASPX:
<telerik:RadComboBox ID="RadComboBox1" runat="server" Width="186px" Label="Countries:" CheckBoxes="true"
    OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged" on AutoPostBack="true"
    Skin="Metro" />
<br />
<br />
<telerik:RadComboBox ID="RadComboBox2" runat="server" Width="186px" Label="Cities from Checked Countries:"
    Skin="Metro" />
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadComboBox1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadComboBox2" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />

C#:
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        LoadCountries();
    }
}
protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    List<string> l1 = new List<string>();
    foreach (var items in RadComboBox1.CheckedItems)
    {
        l1.Add(items.Text); //Adding the RadComboBox Checked items to a List
    }
    if (l1.Count > 0)
    {
        LoadCities(l1);
    }
    else
    {
        RadComboBox2.Items.Clear();
    }
}
 
protected void LoadCountries()
{
    //Binding the First RadCombobox
    SqlConnection connection = new SqlConnection(
    ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
 
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Country ORDER By Name", connection);
    DataTable dt = new DataTable();
    adapter.Fill(dt);
    RadComboBox1.DataTextField = "Name";
    RadComboBox1.DataSource = dt;
    RadComboBox1.DataBind();
}
 
protected void LoadCities(List<string> countryName)
{
    SqlConnection connection = new SqlConnection(
    ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
 
    //Fetching the RadCombobox CheckedItems in a String to give in SQL Query.
    string countries = string.Empty;
    foreach (string countryNames in countryName)
    {
        countries += "'" + countryNames + "',";
    }
    countries = countries.TrimEnd(',');
 
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM City WHERE Countryname  in (" + countries + ") ORDER By Countryname,Cityname", connection);
    DataTable dt = new DataTable();
    adapter.Fill(dt);
    RadComboBox2.DataTextField = "Cityname";
    RadComboBox2.DataSource = dt;
    RadComboBox2.DataBind();
}

Thanks,
Princy.
Tags
ComboBox
Asked by
Mani PC
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or