Imports Telerik.OpenAccess
Imports Telerik.Web.UI
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 o As Object, _
        ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs) _
        Handles ddlCategory.SelectedIndexChanged

        If (TryCast(o, RadComboBox)).SelectedValue <> Nothing Then
            Dim selectValue As System.Nullable(Of Integer) = _
                Int32.Parse((TryCast(o, RadComboBox)).SelectedValue)

            Dim filteredData = From product In Scope.Extent(Of Product)() _
               Where product.CategoryID = selectValue

            gvProducts.DataSource = filteredData.ToList()
            gvProducts.DataBind()
        End If
    End Sub
End Class