<%@ Page Language="C#" %>

<%@ Import Namespace="System.Linq" %>
<%@ 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">

<script runat="server">
    
    DataClassesDataContext context = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RadGrid1.MasterTableView.FilterExpression = "ParentID = 0";
        }
    }

    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = context.SelfReferencings.ToList();
    }

    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        bool shouldSubmitChanges = false;

        if (e.CommandName == RadGrid.DeleteCommandName)
        {
            context.SelfReferencings.DeleteOnSubmit(FindEntityByID(((GridDataItem)e.Item).GetDataKeyValue("ID")));
            shouldSubmitChanges = true;
        }

        if (e.CommandName == RadGrid.UpdateCommandName)
        {
            SelfReferencing entity = FindEntityByID(((GridEditFormItem)e.Item).ParentItem.GetDataKeyValue("ID"));
            ((GridEditFormItem)e.Item).UpdateValues(entity);
            shouldSubmitChanges = true;
        }

        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            SelfReferencing entity = new SelfReferencing();
            ((GridEditFormItem)e.Item).UpdateValues(entity);
            context.SelfReferencings.InsertOnSubmit(entity);
            shouldSubmitChanges = true;
        }

        if (shouldSubmitChanges)
        {
            context.SubmitChanges();
        }
    }

    protected SelfReferencing FindEntityByID(object id)
    {
        return context.SelfReferencings.Where(n => n.ID == (int?)id).FirstOrDefault();
    }

    protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
        if (e.Column.IsBoundToFieldName("ID"))
        {
            ((GridBoundColumn)e.Column).ReadOnly = true;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
        <telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server" />
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" AutoGenerateDeleteColumn="true"
            AutoGenerateEditColumn="true" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand"
            OnColumnCreated="RadGrid1_ColumnCreated">
            <MasterTableView Width="100%" CommandItemDisplay="Top" AutoGenerateColumns="true"
                DataKeyNames="ID" EditMode="PopUp">
                <SelfHierarchySettings ParentKeyName="ParentID" KeyName="ID" />
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>
