Click here to Skip to main content
15,943,148 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone,

I created a gridview in which columns,rows,fields etc are assigned at run time i.e. Dynamically
The class file for generating dynamic template fields is below:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

//A customized class for displaying the Template Column
public class GridViewTemplate : ITemplate
{
    //A variable to hold the type of ListItemType.
    ListItemType _templateType;

    //A variable to hold the column name.
    string _columnName;

    //Constructor where we define the template type and column name.
    public GridViewTemplate(ListItemType type, string colname)
    {
        //Stores the template type.
        _templateType = type;

        //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 lb1 = new LinkButton();                            //Allocates the new text box object.
                lb1.DataBinding += new EventHandler(lb1_DataBinding);   //Attaches the data binding event.
                //tb1.Columns = 4;                                        //Creates a column with size 4.
                container.Controls.Add(lb1);                            //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 lb1_DataBinding(object sender, EventArgs e)
    {
        LinkButton lbdata = (LinkButton)sender;
        lbdata.ForeColor = System.Drawing.Color.White;
        GridViewRow container = (GridViewRow)lbdata.NamingContainer;
        object dataValue = DataBinder.Eval(container.DataItem, _columnName);
        if (dataValue != DBNull.Value)
        {
            lbdata.Text = dataValue.ToString();
           lbdata.Attributes.Add("title", SetToolTip(dataValue.ToString())); 
            lbdata.CssClass = "tTip";
        }

    }
    protected string SetToolTip(string code1)
    {
        if (code1.Length > 9)
        {
            string Code = code1.Substring(0, 4).ToString();

            string Query = "spa_tooltip '"+Code+"' ";
            DataTable dt = LoadIn.ReturnDatatable(Query);

            string data = "<table  width=100%><tr><td>";
            data += "Name</td><td>";
            data += code1.Substring(5).ToString() + "</td></tr>";
            data += "<tr><td>Date</td><td>";
            data += Convert.ToDateTime(dt.Rows[0].ItemArray[0]).ToString("dd/MM/yyyy") + "</td></tr>";
            data += "<tr><td>Treatment</td><td>";
            data += dt.Rows[0].ItemArray[2].ToString() + "</td></tr>";
            data += "<tr><td>Duration</td><td>";
            data += dt.Rows[0].ItemArray[1].ToString() + "</td></tr>";
            data += "<tr><td>Room No</td><td>";
            data += dt.Rows[0].ItemArray[3].ToString() + "</td></tr>";
            data += "<tr><td>Note</td><td>";
            data += dt.Rows[0].ItemArray[9].ToString() + "</td></tr>";
            data += "<tr><td>Booked by</td><td>";
            data += dt.Rows[0].ItemArray[7].ToString() + "</td></tr>";
            data += "<tr><td>Booking Time</td><td>";
            data += dt.Rows[0].ItemArray[8].ToString();
            data += "</td></tr></table>";
            return data;
        }
        else
        {

            return code1;
        }
    }

}

now the gridview is bound to the fields by firing a query from the database and accordingly the fields are generated with the name,time etc fields from the database.

Now there is one linkbutton which is also dynamically generated in the above class file.

The whole gridview is then seen on the page called Default.aspx, Now
I want to open Ajax ModalPopupExtender on click of that linkbutton , and the Ajax ModalPopupExtender is on the Page called Default.aspx.

what do i write to call that modal popup extender (ajax) to show the popup on click of each linkbutton generated.

Plz help me.

Thanks & Regards,
Krunal Panchal
Posted
Updated 12-Apr-11 1:22am
v2
Comments
Toniyo Jackson 12-Apr-11 7:22am    
Added pre tag
hitech_s 24-Sep-11 3:38am    
simple row command you can use i think by giving commandname for dynamically created linkbutton...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900