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 System.Data.OleDb;
using Telerik.WebControls;

namespace Cascade
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadMakes();
        }

        protected void rcbModel_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadModels(e.Text);
        }

        private void LoadMakes()
        {
            string path = Server.MapPath("~/Combobox.mdb");
            OleDbConnection dbCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path);
            dbCon.Open();

            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Make", dbCon);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            dbCon.Close();

            rcbMake.DataTextField = "Make";
            rcbMake.DataValueField = "ID";
            rcbMake.DataSource = dt;
            rcbMake.DataBind();

            rcbMake.Items.Insert(0, new RadComboBoxItem("- Select a make -"));

            foreach (RadComboBoxItem item in rcbMake.Items)
                item.ToolTip = item.Text;
        }

        private void LoadModels( string makeID)
        {
            string path = Server.MapPath("~/Combobox.mdb");
            OleDbConnection dbCon = new OleDbConnection(
                "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path);
            dbCon.Open();

            OleDbDataAdapter adapter = new OleDbDataAdapter(
                "SELECT * FROM Model WHERE MakeID=" + makeID, dbCon);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            dbCon.Close();

            rcbModel.DataTextField = "Model";
            rcbModel.DataValueField = "ID";
            rcbModel.DataSource = dt;
            rcbModel.DataBind();

            rcbModel.Items.Insert(0, new RadComboBoxItem("- Select a model -"));

            foreach (RadComboBoxItem item in rcbModel.Items)
                item.ToolTip = item.Text;
        }
    }
}
