Imports Telerik.OpenAccess
Imports _2_ORM_Model

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Private _scope As IObjectScope = NorthwindProvider.GetNewObjectScope()

    Public Property Scope() As IObjectScope
        Get
            Return _scope
        End Get
        Set(ByVal value As IObjectScope)
            _scope = value
        End Set
    End Property

    Public Overloads Overrides Sub Dispose()
        scope.Dispose()
        MyBase.Dispose()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ddlCategory.DataSource = OpenAccessDataSource1
            ddlCategory.DataTextField = "CategoryName"
            ddlCategory.DataValueField = "CategoryID"
            ddlCategory.DataBind()
        End If
    End Sub

    Protected Sub ddlCategory_SelectedIndexChanged(ByVal sender As Object, _
                  ByVal e As EventArgs) Handles ddlCategory.SelectedIndexChanged

        If (TryCast(sender, DropDownList)).SelectedValue <> Nothing Then

            Dim selectValue As System.Nullable(Of Integer) = _
                Int32.Parse((TryCast(sender, DropDownList)).SelectedValue)

            Dim filteredData = _
             From product In Scope.Extent(Of Product)() _
                Where product.CategoryID.Equals(selectValue) _
                Select _
                 ProductName = product.ProductName, _
                 QuantityPerUnit = product.QuantityPerUnit, _
                 CategoryName = product.Category.CategoryName
            gvProducts.DataSource = filteredData.ToList()
            gvProducts.DataBind()

        End If
    End Sub
End Class