using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for Class1
/// </summary>
public class GridViewTemplate : ITemplate
{

    //A variable to hold the type of ListItemType.

    ListItemType _templateType;



    //A variable to hold the column name.

    string _columnName;

    string commandArgument = "", commandName = "";

    //Constructor where we define the template type and column name.

    public GridViewTemplate(ListItemType type, string colname, string CmdName, string Cmdargument)
    {

        //Stores the template type.

        _templateType = type;

        commandArgument = Cmdargument;

        commandName = CmdName;

        //Stores the column name.

        _columnName = colname;

    }



    void ITemplate.InstantiateIn(System.Web.UI.Control container)
    {

        switch (_templateType)
        {

            case ListItemType.Header:

                //Creates a new label control and add it to the container.

                Label lbl = new Label();            //Allocates the new label object.

                lbl.Text = _columnName;             //Assigns the name of the column in the lable.

                container.Controls.Add(lbl);        //Adds the newly created label control to the container.

                break;



            case ListItemType.Item:

                //Creates a new text box control and add it to the container.

                LinkButton tb1 = new LinkButton();                            //Allocates the new text box object.

                tb1.DataBinding += new EventHandler(tb1_DataBinding);   //Attaches the data binding event.

                tb1.CommandName = commandName;

                tb1.CommandArgument = commandArgument;

                tb1.Style["text-decoration"] = "none";
                tb1.Style["font-family"] = "Tahoma";
                tb1.Style["font-size"] = "8pt";

                container.Controls.Add(tb1);                            //Adds the newly created textbox to the container.

                break;



            case ListItemType.EditItem:

                //As, I am not using any EditItem, I didnot added any code here.

                break;



            case ListItemType.Footer:

                CheckBox chkColumn = new CheckBox();

                chkColumn.ID = "Chk" + _columnName;

                container.Controls.Add(chkColumn);

                break;

        }

    }



    /// <summary>

    /// This is the event, which will be raised when the binding happens.

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    void tb1_DataBinding(object sender, EventArgs e)
    {

        LinkButton txtdata = (LinkButton)sender;

        GridViewRow container = (GridViewRow)txtdata.NamingContainer;

        object dataValue = DataBinder.Eval(container.DataItem, _columnName);

        if (dataValue != DBNull.Value)
        {

            txtdata.Text = dataValue.ToString();

        }

    }

}
