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

public partial class _Default : System.Web.UI.Page
{
    private List<string> ids = new List<string>();

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

        Grd_Url.PreRender += Grd_Url_PreRender;
    }

    void Grd_Url_PreRender(object sender, EventArgs e)
    {
        CsSqlSelect(sender, e);
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
    }

    protected void Page_Load(object s, EventArgs e)
    {
        // Grab the RadComboBox in CommandItemTemplate on Page_Load
    }

    protected void CsSqlSelect(object s, EventArgs e)
    {
        // I get error for the 2 lines below and cannot get a reference to the RadComboBox [Cmb_Lst_Sel]
        // What am I doing wrong?
        GridItem cmdItem = Grd_Url.MasterTableView.GetItems(GridItemType.CommandItem)[0];
        RadComboBox cmbLst = (RadComboBox)cmdItem.FindControl("Cmb_Lst_Sel");

        if (cmbLst.CheckedItems.Count > 0)
        {
            var items = cmbLst.CheckedItems;
            string lst_ids = "";

            foreach (var item in items)
            {
                lst_ids += "'" + item.Value + "'" + ",";

                ids.Add(item.Value);
            }

            lst_ids = lst_ids.Remove(lst_ids.Length - 1);
            Grd_Url.Rebind();
        }

       

        // Once I have the list ids [lst_ids] from the RadComboBox [Cmb_Lst_Sel], 
        // I pass these IDs to SqlDataSource to filter and rebind the RadGrid [Grd_Url]
    }
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("url_id");
        table.Columns.Add("Item");
        table.Columns.Add("Date", typeof(DateTime));
        for (int i = 1; i < 8; i++)
        {
            table.Rows.Add(i, "Item" + i.ToString(), DateTime.Now.AddDays(i));
        }
        if (ids.Count > 0)
        {
            Grd_Url.DataSource = table.AsEnumerable().Where(z => ids.Contains(z["url_id"]));
        }
        else
        {
            Grd_Url.DataSource = table;
        }
    }
}
