using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class _Default : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        RadGrid1.ItemCreated += new GridItemEventHandler(RadGrid1_ItemCreated);
        RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
        RadGrid1.PreRender += new EventHandler(RadGrid1_PreRender);
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (RadGrid1.MasterTableView.IsItemInserted || flag)
        {
            GridCommandItem item = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem).First() as GridCommandItem;
            (item.FindControl("ButtonInitInsert") as Button).Visible = false;
            (item.FindControl("ButtonPerformInsert") as Button).Visible = true;
        }

        if (flag)
        {
            RadGrid1.MasterTableView.InsertItem();
            GridCommandItem item = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem).First() as GridCommandItem;
            (item.FindControl("ButtonInitInsert") as Button).Visible = false;
            (item.FindControl("ButtonPerformInsert") as Button).Visible = true;
        }
    }
    void RadGrid1_PreRender(object sender, EventArgs e)
    {
       
    }

    bool flag = false;
    void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.PerformInsertCommandName)
        {
            GridCommandItem item = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem).First() as GridCommandItem;
            (item.FindControl("ButtonInitInsert") as Button).Visible = false;
            (item.FindControl("ButtonPerformInsert") as Button).Visible = true;
            flag = true;
        }
    }

    void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
       
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
    {
        GridEditableItem item = (GridEditableItem)e.Item;
        String id = item.GetDataKeyValue("ProductID").ToString();

        if (e.Exception != null)
        {
            e.KeepInEditMode = true;
            e.ExceptionHandled = true;
            SetMessage("Product with ID " + id + " cannot be updated. Reason: " + e.Exception.Message);
        }
        else
        {
            SetMessage("Product with ID " + id + " is updated!");
        }
    }

    protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            SetMessage("Product cannot be inserted. Reason: " + e.Exception.Message);
        }
        else
        {
            SetMessage("New product is inserted!");
        }
    }

    protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        String id = dataItem.GetDataKeyValue("ProductID").ToString();

        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            SetMessage("Product with ID " + id + " cannot be deleted. Reason: " + e.Exception.Message);
        }
        else
        {
            SetMessage("Product with ID " + id + " is deleted!");
        }
    }

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

    private void SetMessage(string message)
    {
        gridMessage = message;
    }

    private string gridMessage = null;
    protected void RadGrid1_DataBound(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(gridMessage))
        {
            DisplayMessage(gridMessage);
        }
    }
}
