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;
using System.Collections;
using System.Diagnostics;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    private Stopwatch stp1 = new Stopwatch();
    private Stopwatch stp2 = new Stopwatch();
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        
        stp1.Start();
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    

    protected void RadGrid1_PreRender(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            foreach (GridItem item in RadGrid1.MasterTableView.Items)
            {
                if (item is GridEditableItem)
                {
                    GridEditableItem editableItem = item as GridDataItem;
                    editableItem.Edit = true;
                }
            }
            RadGrid1.Rebind();
            
        }

        stp1.Stop();
    }

    private StringBuilder builder = new StringBuilder();
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "UpdateAll")
        {
            foreach (GridEditFormItem editedItem in RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem))
            {
                Hashtable newValues = new Hashtable();
                e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
                SqlDataSource1.UpdateParameters["UnitsInStock"].DefaultValue = newValues["UnitsInStock"].ToString();
                SqlDataSource1.UpdateParameters["ProductName"].DefaultValue = newValues["ProductName"].ToString();
                SqlDataSource1.UpdateParameters["ProductID"].DefaultValue = editedItem.GetDataKeyValue("ProductID").ToString();
                stp2.Start();
                SqlDataSource1.Update();
                stp2.Stop();
                builder.Append(string.Format("{0} <br/>",stp2.Elapsed));
                stp2 = new Stopwatch();

                editedItem.Edit = false;
            }
        }

    }
    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;
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        //RadGrid1.Rebind();

        Label1.Text = stp1.Elapsed.ToString();
        Label2.Text = builder.ToString();
    }
    private string gridMessage = null;
    protected void RadGrid1_DataBound(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(gridMessage))
        {
            DisplayMessage(gridMessage);
        }
    }
}
