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

public partial class ContentPage : System.Web.UI.Page
{
    private static readonly string sessionKey = "MyCookie";

    private bool HasSettings
    {
        get
        {
            return (Session[sessionKey] != null);
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // If QueryString "KeyName" exists, Load previous state
        if (Request.QueryString["KeyName"] != null && HasSettings)
        {

        }

    }

    #region RadGrid - Data Binding


    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        (sender as RadGrid).DataSource = OrdersTable(); 
    }

    private DataTable OrdersTable()
    {
        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("OrderID", typeof(int)));
        dt.Columns.Add(new DataColumn("OrderDate", typeof(DateTime)));
        dt.Columns.Add(new DataColumn("Freight", typeof(decimal)));
        dt.Columns.Add(new DataColumn("ShipName", typeof(string)));
        dt.Columns.Add(new DataColumn("ShipCountry", typeof(string)));

        dt.PrimaryKey = new DataColumn[] { dt.Columns["OrderID"] };

        for (int i = 0; i < 70; i++)
        {
            int index = i + 1;

            DataRow row = dt.NewRow();

            row["OrderID"] = index;
            row["OrderDate"] = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddHours(index);
            row["Freight"] = index * 0.1 + index * 0.01;
            row["ShipName"] = "Name " + index;
            row["ShipCountry"] = "Country " + index;

            dt.Rows.Add(row);
        }

        return dt;
    }

    #endregion
    protected void btnReloadPage_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }

    protected void btnSaveState_Click(object sender, EventArgs e)
    {
        SaveSettings();
    }

    protected void btnLoadState_Click(object sender, EventArgs e)
    {
        LoadSettings();
        RadGrid1.Rebind();
    }

    protected void btnClearState_Click(object sender, EventArgs e)
    {
        ClearSettings();
    }

    private void SaveSettings()
    {
        var RadPersistenceManager1 = RadPersistenceManager.GetCurrent(Page);
        RadPersistenceManager1.SaveState();

        EnableDisableButtons();
    }
    private void LoadSettings()
    {
        var RadPersistenceManager1 = RadPersistenceManager.GetCurrent(Page);
        RadPersistenceManager1.LoadState();
        RadGrid1.Rebind();

        //EnableDisableButtons();

        //Label1.Text = "Settings for " + sessionKey + " were restored successfully!";
    }

    private void ClearSettings()
    {
        Session[sessionKey] = null;

        EnableDisableButtons();

        Label1.Text = "Settings for " + sessionKey + " deleted successfully!";
    }

    private void EnableDisableButtons()
    {
        btnLoadState.Enabled = HasSettings;
        btnClearState.Enabled = HasSettings;
        btnSaveState.Enabled = !HasSettings;
    }

    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var RadPersistenceManager1 = RadPersistenceManager.GetCurrent(Page);
            RadPersistenceManager1.LoadState();
            RadGrid1.Rebind();
        }
    }
}

public class CustomSessionProvider : IStateStorageProvider
{
    public void SaveStateToStorage(string key, string serializedState)
    {
        // Save the serialized state somewhere
        HttpContext.Current.Session[key] = serializedState;
    }

    public string LoadStateFromStorage(string key)
    {
        return HttpContext.Current.Session[key].ToString();
    }
}