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 RadGridWithEditUserControl : System.Web.UI.Page
{
    public string CustomerIDViewState
    {
        get
        {
            return (string)ViewState["CustomerID"] ?? string.Empty;
        }
        set
        {
            ViewState["CustomerID"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //RadGrid1.
    }
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        using (DataClassesDataContext dc = new DataClassesDataContext())
        {
            RadGrid1.DataSource = dc.Customers.Take(50).ToList();
        }
    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        foreach (GridItem item in RadGrid1.Items)
        {
            // You need to menage the content of the <see href="CustomerIDViewState">, because even you click Update, 
            //the row will stay in edit mode.
            if (item is GridDataItem && ((GridDataItem)item).GetDataKeyValue("CustomerID").ToString() == CustomerIDViewState &&
                item.Edit == false)
            {
                item.Edit = true;
                //Opens the grid back up for the edited row 
                RadGrid1.MasterTableView.Rebind();
                item.Selected = true;
            }
        }
    }
    protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
    {
        //TODO: Not implemented
    }
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.InitInsertCommandName)
        {
            e.Canceled = true;
            RadGrid1.EditIndexes.Clear();

            e.Item.OwnerTableView.EditFormSettings.UserControlName = "EditUserControl.ascx";
            e.Item.OwnerTableView.InsertItem();
        }
        else if (e.CommandName == RadGrid.EditCommandName)
        {
            e.Item.OwnerTableView.IsItemInserted = false;
            e.Item.OwnerTableView.EditFormSettings.UserControlName = "EditUserControl.ascx";
        }
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem) 
        {
            GridDataItem item = (GridDataItem)e.Item;
            HyperLink link = (HyperLink)item["HyperLinkColumn"].Controls[0];
            link.NavigateUrl = "http://www.abv.bg";
            link.Text = "abv";
        }
    }
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode) //if the item is a GridEditFormItem
        {
            var item = e.Item as GridEditFormItem;
            //TODO: find the control and apply the Ajaxsettings in the ItemCreated event handler
            //var control = (Control)item.FindControl("ControlID"); //find the Control in the edit form
            //control.Event += new EventHandler(control_Event); // the required event of the control
            //RadAjaxManager1.AjaxSettings.AddAjaxSetting(control,control); //add Ajax settings, which control will 
            ////initiate postback and which control will be updated
        }
    }
}
