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;
using System.Data;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Page_Init(object sender, EventArgs e)
    {
        RadGrid grid = new RadGrid();
        grid.AutoGenerateColumns = false;
        grid.DataSourceID = "SqlDataSource1";
        string templateColumnName = "ContactName";
        GridTemplateColumn templateColumn = new GridTemplateColumn();
        templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
        templateColumn.HeaderTemplate = new MyHeaderTemplate(templateColumnName);
        templateColumn.HeaderText = templateColumnName;
        grid.MasterTableView.Columns.Add(templateColumn);
        grid.AllowPaging = true;
        grid.PageSize = 3;
        grid.Skin = "Outlook";
        PlaceHolder1.Controls.Add(grid);
    }

    private class MyHeaderTemplate : ITemplate
    {
        private string colname;
        public MyHeaderTemplate(string cName)
        {
            colname = cName;
        }
        public void InstantiateIn(System.Web.UI.Control container)
        {
            LinkButton label = new LinkButton();
            label.Text = colname;
            label.CommandName = "Sort";
            label.CommandArgument = colname;
            container.Controls.Add(label);
        }
    }

    private class MyTemplate : ITemplate
    {
        protected LiteralControl lControl;
        protected HyperLink searchGoogle;
        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);
            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 />"));
        }
        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 />";
        }
    }


}