using System;
using System.Web.UI.WebControls;
using Model;
using Telerik.OpenAccess;
using Telerik.OpenAccess.Query;
using Telerik.Web.UI;

namespace MyWebApp
{
  public partial class _Default : System.Web.UI.Page
  {
    private IObjectScope scope = NorthwindProvider.GetNewObjectScope();

    public IObjectScope Scope
    {
      get { return scope; }
      set { scope = value; }
    }

    public override void Dispose()
    {
      scope.Dispose();
      base.Dispose();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        ddlCategory.DataSource = OpenAccessDataSource1;
        ddlCategory.DataTextField = "CategoryName";
        ddlCategory.DataValueField = "CategoryID";
        ddlCategory.DataBind();
      }
    }

    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void ddlCategory_SelectedIndexChanged1(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
      if ((o as RadComboBox).SelectedValue != null)
      {
        int? selectValue = (int?)Int32.Parse((o as RadComboBox).SelectedValue);

        var filteredData = from product in scope.Extent<Product>()
                           where product.CategoryID == selectValue
                           select new
                           {
                             Product = product.ProductName,
                             QuantityPerUnit = product.QuantityPerUnit,
                             Category = product.Category.CategoryName
                           };
        gvProducts.DataSource = filteredData.ToList();
        gvProducts.DataBind();
      }
    }

  }
}
