using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _2_ORM_Model;
using Telerik.OpenAccess;

namespace _3_ORM_MyWebApp
{
    public partial class _Default : System.Web.UI.Page
    {
        private IObjectScope scope = NorthwindProvider.GetNewObjectScope();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlCategory.DataSource = OpenAccessDataSource1;
                ddlCategory.DataTextField = "CategoryName";
                ddlCategory.DataValueField = "CategoryID";
                ddlCategory.DataBind();
            }

        }

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

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

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((sender as DropDownList).SelectedValue != null)
            {
                int? selectValue =
                  (int?)Int32.Parse((sender as DropDownList).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();
            }

        }
    }
}
