using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.Data.SqlClient;

public partial class RelatedCombos_Grid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void rcbContinents_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        RadComboBox continentsCombo = o as RadComboBox;
        
        //save the combo selected value
        ViewState["continentsComboValue"] = continentsCombo.SelectedValue;        
        
        //filter the grid
        RadGrid1.MasterTableView.FilterExpression = "ContinentName LIKE '%" + continentsCombo.SelectedValue + "%'";
        RadGrid1.Rebind();        
    }

    protected void rcbContinents_PreRender(object sender, EventArgs e)
    {
        //persist the combo selected value
        if (ViewState["continentsComboValue"] != null)
        {
            RadComboBox continentsCombo = sender as RadComboBox;
            continentsCombo.SelectedValue = ViewState["continentsComboValue"].ToString();
        }
    }
    protected void rcbCountries_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    {
        RadComboBox countriesCombo = sender as RadComboBox;
        string continentValue = ViewState["continentsComboValue"] != null ? ViewState["continentsComboValue"].ToString() : "";
        countriesCombo.DataSource = LoadCountries(continentValue);
        countriesCombo.DataBind();
        countriesCombo.Items.Insert(0, new RadComboBoxItem("All", ""));
    }
    protected void rcbCountries_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        RadComboBox countriesCombo = o as RadComboBox;

        //save the combo selected value
        ViewState["countriesComboValue"] = countriesCombo.SelectedValue;

        //filter the grid
        RadGrid1.MasterTableView.FilterExpression = "CountryName LIKE '%" + countriesCombo.SelectedValue + "%'";
        RadGrid1.Rebind(); 
    }
    protected void rcbCountries_PreRender(object sender, EventArgs e)
    {
        //persist the combo selected value
        if (ViewState["countriesComboValue"] != null)
        {
            RadComboBox continentsCombo = sender as RadComboBox;
            continentsCombo.SelectedValue = ViewState["countriesComboValue"].ToString();
        }
    }
    protected void rcbCities_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    {
        RadComboBox citiesCombo = sender as RadComboBox;
        string continentValue = ViewState["continentsComboValue"] != null ? ViewState["continentsComboValue"].ToString() : "";
        string countryValue = ViewState["countriesComboValue"] != null ? ViewState["countriesComboValue"].ToString() : "";
        citiesCombo.DataSource = LoadCities(continentValue, countryValue);
        citiesCombo.DataBind();
        citiesCombo.Items.Insert(0, new RadComboBoxItem("All", ""));
    }
    protected void rcbCities_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        RadComboBox citiesCombo = o as RadComboBox;

        //save the combo selected value
        ViewState["citiesComboValue"] = citiesCombo.SelectedValue;

        //filter the grid
        RadGrid1.MasterTableView.FilterExpression = "CityName LIKE '%" + citiesCombo.SelectedValue + "%'";
        RadGrid1.Rebind();
    }
    protected void rcbCities_PreRender(object sender, EventArgs e)
    {
        //persist the combo selected value
        if (ViewState["citiesComboValue"] != null)
        {
            RadComboBox continentsCombo = sender as RadComboBox;
            continentsCombo.SelectedValue = ViewState["citiesComboValue"].ToString();
        }
    }
    protected DataTable LoadCountries(string continent)
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ComboGrid"].ConnectionString);

        //select a country based on the continentID
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT CountryName FROM [ViewAllUsers] WHERE ContinentName LIKE '%" + continent + "%'", connection);

        DataTable dt = new DataTable();
        adapter.Fill(dt);

        return dt;
    }

    protected DataTable LoadCities(string continent, string country)
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ComboGrid"].ConnectionString);

        //select a city based on the countryID
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT CityName FROM [ViewAllUsers] WHERE ContinentName LIKE '%" + continent + "%' and CountryName LIKE '%" + country + "%'", connection);

        DataTable dt = new DataTable();
        adapter.Fill(dt);

        return dt;
    }    
}
