<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientServerExpand.aspx.cs"
    Inherits="_Default" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>

        <script type="text/javascript">
            function expanding(sender, args)
            {
                var master = sender.get_masterTableView();
                var items = master.get_dataItems();
                var thisItem = items[args.get_itemIndexHierarchical()];

                $get("MyHiddenState").value = thisItem.getDataKeyValue("CategoryID");

                setTimeout(function()
                {
                    var nestedRow = thisItem.get_element().nextSibling;
                    
                    var nestedGrid = $find(nestedRow.getElementsByTagName("DIV")[0].control.get_id());
                    var nestedMaster = nestedGrid.get_masterTableView();

                    if (nestedMaster.get_dataItems().length == 0)
                    {
                        var refreshButton = nestedRow.getElementsByTagName("input")[0];
                        refreshButton.click();
                    }
                }, 0);
            }
        </script>
        
        <asp:HiddenField runat="server" ID="MyHiddenState" />
        
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" EnableAJAX="false"
            DefaultLoadingPanelID="RadAjaxLoadingPanel1">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                        <telerik:AjaxUpdatedControl ControlID="Panel1" />
                        <telerik:AjaxUpdatedControl ControlID="MyHiddenState" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Vista"
            Transparency="30">
        </telerik:RadAjaxLoadingPanel>
        
        <asp:Panel ID="Panel1" runat="server" style="float:right;">        
        </asp:Panel>
        
        <telerik:RadGrid ID="RadGrid1" runat="server" Skin="Vista" Width="800px" AutoGenerateColumns="true"
            DataSourceID="CategoriesDataSource">
            <MasterTableView DataSourceID="CategoriesDataSource" 
                DataKeyNames="CategoryID" ClientDataKeyNames="CategoryID"
                HierarchyLoadMode="Client"
                HierarchyDefaultExpanded="false">
                <NestedViewTemplate>
                    <asp:Button ID="RefreshButton" runat="server" OnClick="RefreshButton_Click" 
                        style="display:none;"/>
                    <telerik:RadGrid ID="ProductsGrid" runat="server" Skin="Vista" 
                        OnNeedDataSource="ProductsGrid_NeedDataSource">
                        <MasterTableView DataKeyNames="CategoryID, ProductID">
                        </MasterTableView>
                    </telerik:RadGrid>
                </NestedViewTemplate>
            </MasterTableView>
            <ClientSettings>
                <ClientEvents OnHierarchyExpanding="expanding" />
            </ClientSettings>
        </telerik:RadGrid>
        
        <asp:AccessDataSource ID="CategoriesDataSource" runat="server" DataFile="~/App_Data/Nwind.mdb"
            SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
        </asp:AccessDataSource>
        
        <asp:AccessDataSource ID="ProductsDataSource" runat="server" DataFile="~/App_Data/Nwind.mdb"
            SelectCommand="SELECT [ProductID], [ProductName], [CategoryID], [UnitPrice], [UnitsInStock] FROM [Products] WHERE [CategoryID] = ?">
            <SelectParameters>
                <asp:Parameter Name="CategoryID" Type="Int32" DefaultValue="-1" />
            </SelectParameters>
        </asp:AccessDataSource>
    </div>
    </form>
</body>
</html>
