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 void Page_Load(object sender, EventArgs e)
    {

        Button2.Enabled = Session[UserSelection.SelectedValue] != null ? true : false;
    }

    //protected void Pre_Render(object sender, EventArgs e)
    //{
        
    //    Button2.Enabled = Session[UserSelection.SelectedValue] != null ? true : false;
    //}

    protected void Button1_Click(object sender, EventArgs e)
    {
        string user = UserSelection.SelectedValue;
        GridSettingsPersister SavePersister = new GridSettingsPersister(RadGrid1);
        Session[user] = SavePersister.SaveSettings();
        InfoImage.Visible = true;
        StatusLabel.Text = "Settings for " + user + " saved successfully!";
        Button2.Enabled = true;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string user = UserSelection.SelectedValue;
        GridSettingsPersister LoadPersister = new GridSettingsPersister(RadGrid1);

        if (Session[user] == null)
        {
            InfoImage.Visible = true;
            StatusLabel.Text = "No saved settings for this user were found!";
        }
        else
        {
            string settings = (string)Session[user];
            LoadPersister.LoadSettings(settings);
            RadGrid1.Rebind();
            InfoImage.Visible = true;
            StatusLabel.Text = "Settings for " + user + " were restored successfully!";
        }
    }

    protected void CustomizedGridPagerSize(object sender,GridItemEventArgs e)
    {
        if (e.Item is GridPagerItem)
        {
            int allItemsCount = (e.Item as GridPagerItem).Paging.DataSourceCount;
            RadComboBox combo = (e.Item as GridPagerItem).FindControl("PageSizeComboBox") as RadComboBox;
            RadComboBoxItem item1 = new RadComboBoxItem("10", "10");
            RadComboBoxItem item2 = new RadComboBoxItem("20", "20");
            RadComboBoxItem item3 = new RadComboBoxItem("50", "50");
            RadComboBoxItem item4 = new RadComboBoxItem("100", "100");
            RadComboBoxItem item5 = new RadComboBoxItem("200", "200");
            RadComboBoxItem item6 = new RadComboBoxItem("All", allItemsCount.ToString());
            item1.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
            item2.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
            item3.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
            item4.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
            item5.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
            item6.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
            combo.Items.Clear();
            combo.Items.Add(item1);
            combo.Items.Add(item2);
            combo.Items.Add(item3);
            combo.Items.Add(item4);
            combo.Items.Add(item5);
            combo.Items.Add(item6);
            combo.Items.FindItemByValue(((RadGrid)e.Item.OwnerTableView.Parent).PageSize.ToString()).Selected = true;
        }
    }
}
