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)
    {
        RadGrid1.PreRender +=new EventHandler(RadGrid1_PreRender);
    }

    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;

    }


    protected void RadGrid1_PreRender( object sender,EventArgs e )
    {
        string filterExpression = "";
        bool isSet = false;

        if (ViewState["citiesComboValue"] as string != "")
        {
            filterExpression = "CityName LIKE '%" + ViewState["citiesComboValue"] + "%'";
            isSet = true;
        }
        if (ViewState["countriesComboValue"] as string != "")
        {
            if (isSet)
            {
                filterExpression += " AND CountryName LIKE '%" + ViewState["countriesComboValue"] + "%'";
            }
            else
            {
                filterExpression = "CountryName LIKE '%" + ViewState["countriesComboValue"] + "%'";
            }
            isSet = true;
        }

        if (ViewState["continentsComboValue"] as string != "")
        {
            if (isSet)
            {
                filterExpression += " AND ContinentName LIKE '%" + ViewState["continentsComboValue"] + "%'";
            }
            else
            {
                filterExpression = "ContinentName LIKE '%" + ViewState["continentsComboValue"] + "%'";
            }
            isSet = true;
        }

        RadGrid1.MasterTableView.FilterExpression = filterExpression;

        RadGrid1.Rebind();
    }

    protected void rcbContinents_PreRender(object sender, EventArgs e)
    {

        (sender as RadComboBox).SelectedValue = ViewState["continentsComboValue"] as string;
        (sender as RadComboBox).Text = ViewState["continentsComboValue"] as string;
     
    }
    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;

        ViewState["countriesComboValue"] = countriesCombo.SelectedValue;

    }
    protected void rcbCountries_PreRender(object sender, EventArgs e)
    {
        (sender as RadComboBox).SelectedValue = ViewState["countriesComboValue"] as string;
        (sender as RadComboBox).Text = ViewState["countriesComboValue"] as string;

    }
    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;

        ViewState["citiesComboValue"] = citiesCombo.SelectedValue;

    }
    protected void rcbCities_PreRender(object sender, EventArgs e)
    {
        (sender as RadComboBox).SelectedValue = ViewState["citiesComboValue"] as string;
        (sender as RadComboBox).Text = ViewState["citiesComboValue"] as string;
        
    }
    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;
    }
}
