using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class _Default : System.Web.UI.Page
{
    private void Page_Load(object sender, EventArgs e)
    {
    }

    protected void RadGrid1_ItemComand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {

        if (e.CommandName == RadGrid.ExpandCollapseCommandName)
        {
            if (e.Item.OwnerTableView.Name == "Customers")
            {
                GridCommandItem commandItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
                LinkButton diagbtnAdd = (LinkButton)commandItem.FindControl("LinkButtonAdd");

                if (!e.Item.Expanded)
                {
                    diagbtnAdd.Enabled = false;
                }
                else
                {
                    diagbtnAdd.Enabled = true;
                }

            }

            if (e.Item.OwnerTableView.Name == "Orders")
            {
                foreach (GridDataItem masterItem in RadGrid1.MasterTableView.Items)
                {
                    if (masterItem.Expanded)
                    {
                        GridCommandItem commandItem = (GridCommandItem)masterItem.ChildItem.NestedTableViews[0].GetItems(GridItemType.CommandItem)[0];
                        LinkButton visitbtnAdd = (LinkButton)commandItem.FindControl("DetailLinkButtonAdd");
                        visitbtnAdd.Enabled = !visitbtnAdd.Enabled;
                    }
                }

            }
        }





    }

    protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
    {
        string item = getItemName(e.Item.OwnerTableView.Name);
        string field = getFieldName(e.Item.OwnerTableView.Name);
        if (e.Exception != null)
        {
            e.KeepInEditMode = true;
            e.ExceptionHandled = true;
            DisplayMessage(item + " " + e.Item[field].Text + " cannot be updated. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(item + " " + e.Item[field].Text + " updated");
        }
    }

    protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
    {
        string item = getItemName(e.Item.OwnerTableView.Name);
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            DisplayMessage(item + " cannot be inserted. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(item + " inserted");
        }
    }

    protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
    {
        string item = getItemName(e.Item.OwnerTableView.Name);
        string field = getFieldName(e.Item.OwnerTableView.Name);
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            DisplayMessage(item + " " + e.Item[field].Text + " cannot be deleted. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(item + " " + e.Item[field].Text + " deleted");
        }
    }

    protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
    {
        if ("Orders".Equals(e.Item.OwnerTableView.Name))
        {
            GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
            SessionDataSource2.InsertParameters["CustomerID"].DefaultValue = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["CustomerID"].ToString();
        }
        else if ("Details".Equals(e.Item.OwnerTableView.Name))
        {
            GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
            SessionDataSource3.InsertParameters["OrderID"].DefaultValue = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["OrderID"].ToString();
        }
    }

    private String getItemName(string tableName)
    {
        switch (tableName)
        {
            case ("Customers"):
                {
                    return "Customer";
                }
            case ("Orders"):
                {
                    return "Order";
                }
            case ("Details"):
                {
                    return "Details for order";
                }
            default: return "";
        }
    }

    private String getFieldName(string tableName)
    {
        switch (tableName)
        {
            case ("Customers"):
                {
                    return "CustomerID";
                }
            case ("Orders"):
                {
                    return "OrderID";
                }
            case ("Details"):
                {
                    return "OrderID";
                }
            default: return "";
        }
    }

    private void DisplayMessage(string text)
    {
        RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
    }

}
