Radcombox with checkboxes selection

2 posts, 0 answers
  1. Mani PC
    Mani PC avatar
    73 posts
    Member since:
    Jul 2009

    Posted 03 Apr 2013 Link to this post

    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

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 04 Apr 2013 Link to this post

    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.
Back to Top