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

public partial class _Default : Page
{
    protected bool _disableEditForm = true;

    public Boolean DisableEditForm
    {
        get { return _disableEditForm; }
        set { _disableEditForm = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        DetailsGrid.EditIndexes.Add(0);
    }

    protected void DetailsGrid_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor) item.EditManager.GetColumnEditor("Price");
            TableCell cell = (TableCell) editor.TextBoxControl.Parent;

            RequiredFieldValidator requiredValidator = new RequiredFieldValidator();
            editor.TextBoxControl.ID = "priceId";
            requiredValidator.ControlToValidate = editor.TextBoxControl.ID;
            requiredValidator.ErrorMessage = "Enter price for this product";
            cell.Controls.Add(requiredValidator);

            RangeValidator rangeValidator = new RangeValidator();
            rangeValidator.Type = ValidationDataType.Integer;
            rangeValidator.MinimumValue = "1";
            rangeValidator.MaximumValue = "10000";
            rangeValidator.ControlToValidate = editor.TextBoxControl.ID;
            rangeValidator.ErrorMessage = "Enter price between 1 and 10000";
            cell.Controls.Add(rangeValidator);

            editor = (GridTextBoxColumnEditor) item.EditManager.GetColumnEditor("Quantity");
            cell = (TableCell) editor.TextBoxControl.Parent;

            rangeValidator = new RangeValidator();
            editor.TextBoxControl.ID = "quantityId";
            rangeValidator.Type = ValidationDataType.Integer;
            rangeValidator.MinimumValue = "1";
            rangeValidator.MaximumValue = "200";
            rangeValidator.ControlToValidate = editor.TextBoxControl.ID;
            rangeValidator.ErrorMessage = "Enter quantity between 1 and 200";
            cell.Controls.Add(rangeValidator);
        }
        else if (e.Item is GridDataItem)
        {
            e.Item.Style["display"] = "none";
        }
    }

    protected void DetailsGrid_ItemInserted(object source, GridInsertedEventArgs e)
    {
        e.KeepInInsertMode = true;
    }

    protected void DetailsGrid_ItemUpdated(object source, GridUpdatedEventArgs e)
    {
        e.KeepInEditMode = true;
    }

    protected void DetailsGrid_PreRender(object sender, EventArgs e)
    {
        GridDataItem dataItem = DetailsGrid.MasterTableView.Items[0];
        dataItem.Visible = false;

        GridEditFormItem editItem = dataItem.EditFormItem;

        if (this.DisableEditForm)
        {
            foreach (WebControl ctrl in editItem.Controls)
            {
                ctrl.Enabled = false;
            }

            LinkButton updateButton;
            LinkButton cancelButton;


            if (editItem.FindControl("UpdateButton") != null)
            {
                updateButton = editItem.FindControl("UpdateButton") as LinkButton;
                cancelButton = editItem.FindControl("CancelButton") as LinkButton;
            }
            else
            {
                GridEditableItem insertedItem = DetailsGrid.MasterTableView.GetInsertItem() as GridEditableItem;
                updateButton = insertedItem.FindControl("PerformInsertButton") as LinkButton;
                cancelButton = insertedItem.FindControl("CancelButton") as LinkButton;
            }

            updateButton.Visible = false;
            cancelButton.Visible = false;
        }
        else
        {
            this.DisableEditForm = true;
        }
    }

    protected void DetailsGrid_CancelCommand(object source, GridCommandEventArgs e)
    {
        if (!e.Item.OwnerTableView.IsItemInserted)
        {
            e.Canceled = true;
        }
    }

    protected void DetailsGrid_InsertCommand(object source, GridCommandEventArgs e)
    {
        DetailsGrid.MasterTableView.ClearEditItems();
    }

    protected void DetailsGrid_ItemCommand(object source, GridCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case RadGrid.UpdateCommandName:
                this.DisableEditForm = true;
                BusinessGrid.Rebind();
                break;
            case RadGrid.PerformInsertCommandName:
                this.DisableEditForm = true;
                BusinessGrid.MasterTableView.CurrentPageIndex = BusinessGrid.MasterTableView.PageCount - 1;
                BusinessGrid.Rebind();
                break;
        }
    }

    protected void BusinessGrid_ItemCommand(object source, GridCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "EditDetail":
                this.DisableEditForm = false;
                DetailsGrid.Rebind();
                break;
            case "AddDetail":
                DetailsGrid.MasterTableView.ClearEditItems();
                DetailsGrid.MasterTableView.IsItemInserted = true;
                this.DisableEditForm = false;
                DetailsGrid.Rebind();
                break;
            case "RowClick":
                if (DetailsGrid.MasterTableView.IsItemInserted)
                {
                    DetailsGrid.MasterTableView.IsItemInserted = false;
                    DetailsGrid.Rebind();
                }
                
                break;
            default:
                break;
        }
    }
}