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

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>

        <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="true" AllowSorting="true" AllowFilteringByColumn="true" AutoGenerateColumns="false"
            Skin="WebBlue" EnableEmbeddedSkins="true">
            <MasterTableView HierarchyLoadMode="Client">
                <Columns>
                    <telerik:GridNumericColumn DataField="EmployeeID" HeaderText="EmployeeID">
                    </telerik:GridNumericColumn>
                    <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="LastName" HeaderText="LastName">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Title" HeaderText="Title">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="TitleOfCourtesy" HeaderText="TitleOfCourtesy">
                    </telerik:GridBoundColumn>
                    <telerik:GridDateTimeColumn DataField="BirthDate" DataFormatString="{0:MM/dd/yyyy}"
                        HeaderText="BirthDate">
                    </telerik:GridDateTimeColumn>
                </Columns>
                <NestedViewTemplate>
                    <asp:TextBox ID="txtError" runat="server" Text="test"></asp:TextBox>
                </NestedViewTemplate>
            </MasterTableView>
            <ClientSettings>
                <ClientEvents OnCommand="RadGrid1_Command" OnRowDataBound="rowDataBound"></ClientEvents>
                <Resizing AllowColumnResize="true" />
            </ClientSettings>
        </telerik:RadGrid>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"></asp:SqlDataSource>
        <asp:Panel ID="Panel1" runat="server">
        </asp:Panel>

        <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
            <script type="text/javascript">
                function pageLoad(sender, eventArgs) {
                    var tableView = $find("<%= RadGrid1.ClientID %>").get_masterTableView();

                    PageMethods.GetData(0, tableView.get_pageSize(), tableView.get_sortExpressions().toString(), tableView.get_filterExpressions().toList(), updateGrid);
                    PageMethods.GetCount(tableView.get_filterExpressions().toList());
                }

                function RadGrid1_Command(sender, args) {
                    args.set_cancel(true);

                    var pageSize = sender.get_masterTableView().get_pageSize();
                    var sortExpressions = sender.get_masterTableView().get_sortExpressions();
                    var filterExpressions = sender.get_masterTableView().get_filterExpressions();
                    var currentPageIndex = sender.get_masterTableView().get_currentPageIndex();

                    if (args.get_commandName() == "Filter") {
                        currentPageIndex = 0;
                    }

                    var sortExpressionsAsSQL = sortExpressions.toString();

                    PageMethods.GetData(currentPageIndex * pageSize, pageSize, sortExpressionsAsSQL, filterExpressions.toList(), updateGrid);

                    if (args.get_commandName() == "Filter") {
                        PageMethods.GetCount(filterExpressions.toList());
                    }
                }

                function updateGrid(result) {
                    var tableView = $find("<%= RadGrid1.ClientID %>").get_masterTableView();

                    tableView.set_dataSource(result);
                    tableView.dataBind();
                }

                function rowDataBound(sender, args) {
                    var grid = $find("<%= RadGrid1.ClientID %>");
                    var masterTable = grid.get_masterTableView();
                    var dataItems = masterTable.get_dataItems();

                    var txtBox;

                    for (var i = 0; i < dataItems.length; i++) {
                        var row = dataItems[i].get_element().nextSibling;
                        txtBox = $telerik.findElement(row, "txtError");
                        //txtBox.value = i + 1;

                        if (i % 2 == 0) {
                            dataItems[i].set_expanded(true);
                        }
                    }
                }
            </script>
        </telerik:RadCodeBlock>
    </form>
</body>
</html>
