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

public partial class _Default : System.Web.UI.Page
{
    private static int parentItemIndex = -1;

    protected void ProductsGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        RadGrid thisGrid = source as RadGrid;
        int catId = -1;

        if (!Int32.TryParse(Request["MyHiddenState"],out catId))
        {
            thisGrid.ShowHeader = false;
            ProductsDataSource.SelectParameters["CategoryID"].DefaultValue = "-1";
        }
        else
        {
            GridNestedViewItem nestedItem = thisGrid.NamingContainer as GridNestedViewItem;
            GridDataItem parentItem = nestedItem.OwnerTableView.FindItemByKeyValue("CategoryID", catId);

            Panel1.Controls.Add(new LiteralControl(String.Format("Parent CategoryID: {0} <br />", parentItem["CategoryID"].Text)));
            
            if (parentItem.Expanded)
            {
                ProductsDataSource.SelectParameters["CategoryID"].DefaultValue = catId.ToString();
            }
        }

        thisGrid.DataSource = ProductsDataSource.Select(new DataSourceSelectArguments());
    }

    protected void RefreshButton_Click(object sender, EventArgs e)
    {
        ((sender as Button).NamingContainer.FindControl("ProductsGrid") as RadGrid).Rebind();
    }
}
