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 System.Data.SqlClient;
using System.Configuration;
using Telerik.Web.UI;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        RadGrid grid = new RadGrid();
        grid.AutoGenerateColumns = false;
        grid.DataSourceID = "SqlDataSource1";
        grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.TopAndBottom;
        grid.MasterTableView.CommandItemTemplate = new CommandItemTemplate();
        grid.AllowFilteringByColumn = true;
        string templateColumnName = "ContactName";
        GridTemplateColumn templateColumn = new GridTemplateColumn();
        templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
        templateColumn.HeaderText = templateColumnName;
        GridBoundColumn boundColumn1 = new GridBoundColumn();
        boundColumn1.DataField = "ContactName";
        boundColumn1.UniqueName = "ConactName";
        boundColumn1.HeaderText = "Bound Column";
        grid.MasterTableView.Columns.Add(templateColumn);
        grid.MasterTableView.Columns.Add(boundColumn1);
        grid.AllowPaging = true;
        grid.PageSize = 3;
        grid.Skin = "Outlook";
        PlaceHolder1.Controls.Add(grid);
    }


    public class CommandItemTemplate : ITemplate
    {
        protected Button button1;
        public CommandItemTemplate()
        {
            
        }

        public void InstantiateIn(Control container)
        {
            //ContentPanel is an aspPanel which contains every control created on the page Init

            button1 = new Button();
            button1.ID = "Button1";
            button1.Text = "Click me";
            button1.Click += button1_Click;
            container.Controls.Add(button1);
        }

        void button1_Click(object sender, EventArgs e)
        {
        }
    }

    private class MyTemplate : ITemplate
    {
        protected LiteralControl lControl;
        protected HyperLink searchGoogle;
        protected CheckBox boolValue;
        private string colname;
        public MyTemplate(string cName)
        {
            colname = cName;
        }
        public void InstantiateIn(System.Web.UI.Control container)
        {
            lControl = new LiteralControl();
            lControl.ID = "lControl";
            lControl.DataBinding += new EventHandler(lControl_DataBinding);
        
            searchGoogle = new HyperLink();
            searchGoogle.ID = "searchGoogle";
            searchGoogle.DataBinding += new EventHandler(searchGoogle_DataBinding);
            boolValue = new CheckBox();
            boolValue.ID = "boolValue";
            boolValue.DataBinding += new EventHandler(boolValue_DataBinding);
            boolValue.Enabled = false;
            Table table = new Table();
            TableRow row1 = new TableRow();
            TableRow row2 = new TableRow();
            TableCell cell11 = new TableCell();
            TableCell cell12 = new TableCell();
            TableCell cell21 = new TableCell();
            TableCell cell22 = new TableCell();
            row1.Cells.Add(cell11);
            row1.Cells.Add(cell12);
            row2.Cells.Add(cell21);
            row2.Cells.Add(cell22);
            table.Rows.Add(row1);
            table.Rows.Add(row2);
            cell11.Text = colname + ": ";
            cell12.Controls.Add(lControl);
            cell21.Text = "Search Google for: ";
            cell22.Controls.Add(searchGoogle);
            container.Controls.Add(table);
            container.Controls.Add(new LiteralControl("<br />"));
            container.Controls.Add(boolValue);
        }
        void boolValue_DataBinding(object sender, EventArgs e)
        {
            CheckBox cBox = (CheckBox)sender;
            GridDataItem container = (GridDataItem)cBox.NamingContainer;
            //cBox.Checked = (bool)((DataRowView)container.DataItem)["Bool"];
        }
        void searchGoogle_DataBinding(object sender, EventArgs e)
        {
            HyperLink link = (HyperLink)sender;
            GridDataItem container = (GridDataItem)link.NamingContainer;
            link.Text = ((DataRowView)container.DataItem)[colname].ToString();
            link.NavigateUrl = "http://www.google.com/search?hl=en&q=" + ((DataRowView)container.DataItem)["ContactName"].ToString() + "&btnG=Google+Search";
        }
        public void lControl_DataBinding(object sender, EventArgs e)
        {
            LiteralControl l = (LiteralControl)sender;
            GridDataItem container = (GridDataItem)l.NamingContainer;
            l.Text = ((DataRowView)container.DataItem)[colname].ToString() + "<br />";
        }
    }
}



 

