vigneshwa ran

vigneshwa ran

  • NA
  • 5
  • 2.2k

adding event handler to button inside gridview using delegat

Jan 31 2014 4:05 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication28
{

public partial class WebForm7 : System.Web.UI.Page
{
DataTable dtt = new DataTable();
public void temp()
{
dtt.Columns.Add("name", typeof(string));
dtt.Columns.Add("id", typeof(string));
dtt.Columns.Add("unit", typeof(string));
dtt.Columns.Add("sex", typeof(string));
dtt.Columns.Add("pic", typeof(string));
DataRow dr = dtt.NewRow();
dr["name"] = "txt";
dr["id"] = "txt";
dr["unit"] = "pic";
dr["sex"] = "ddl!male!female";
dr["pic"] = "pic";
dtt.Rows.Add(dr);
this.WebUserControl11.Number = dtt;
}
    protected void Page_Load(object sender, EventArgs e)
{
TemplateHandler1 t=new TemplateHandler1("q",1,"");
t.gridbtnclickevent += new gridbtnclick(t_gridbtnclickevent);
temp();
}

void t_gridbtnclickevent(object sender, EventArgs e)// this event is not firing
{
throw new NotImplementedException();
}


}

}


my user control ascx(page)

namespace WebApplication28
{
public delegate void gridbtnclick(object sender, EventArgs e);
public class TemplateHandler1 : ITemplate
{
public event gridbtnclick gridbtnclickevent;
string type, colname; int id;
List<string> list = new List<string>();
public TemplateHandler1(string type, int id, string colname,params string[] arr)
{
this.type = type;
this.id = id;
this.colname = colname;
list = new List<string>(arr);
}

void ITemplate.InstantiateIn(Control container)
{
TextBox txt = new TextBox();
Button btn = new Button();
switch (type)
{
case "txt":
txt.ID = "txt"+id.ToString ();
txt.DataBinding += new EventHandler(tb1_DataBinding);
container.Controls.Add(txt);
break;
case "btn":
btn.ID = "btn" + id.ToString();
container.Controls.Add(btn);
break;
case "ddl":
DropDownList ddl = new DropDownList();
ddl.ID = "ddl" + id.ToString();
ddl.DataBinding += new EventHandler(ddl_DataBinding);
foreach (string word in list)
{
ddl.Items.Add(word);
}
ddl.Items.RemoveAt(0);
container.Controls.Add(ddl);
break;
case "pic":
txt.ID = "txt" + id.ToString();
txt.DataBinding += new EventHandler(tb1_DataBinding);
container.Controls.Add(txt);
btn.ID = "btn" + id.ToString();
container.Controls.Add(btn);
btn.Click += new EventHandler(btn_Click);
break;

}
}

void btn_Click(object sender, EventArgs e)
{
gridbtnclickevent(this, e);
}
void tb1_DataBinding(object sender, EventArgs e)
{
TextBox txtdata = (TextBox)sender;
GridViewRow container = (GridViewRow)txtdata.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, colname);
if (dataValue != DBNull.Value)
{
txtdata.Text = dataValue.ToString();
}
}
void ddl_DataBinding(object sender, EventArgs e)
{
DropDownList txtdata = (DropDownList)sender;
GridViewRow container = (GridViewRow)txtdata.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, colname);
if (dataValue != DBNull.Value)
{
txtdata.Text = dataValue.ToString();
}
}
protected void Dynamic_Method(object sender, EventArgs e)
{
((Button)sender).Text = "Hellooooo";
}
}


public partial class WebUserControl1 : System.Web.UI.UserControl
{
DataTable dt = new DataTable();
DataTable GetDataTable(GridView dtg)
{
// DataTable dt = new DataTable();
dt.Clear(); dt.Columns.Clear();
// add the columns to the datatable
if (dtg.HeaderRow != null)
{

for (int i = 0; i < dtg.HeaderRow.Cells.Count; i++)
{
dt.Columns.Add(dtg.HeaderRow.Cells[i].Text);
}
}

// add each of the data rows to the table
foreach (GridViewRow row in dtg.Rows)
{
DataRow dr;
dr = dt.NewRow();

for (int i = 0; i < row.Cells.Count; i++)
{
if (row.Cells[i].Controls[0].ToString() == "System.Web.UI.WebControls.TextBox")
{
if (row.FindControl("txt" + i) as TextBox != null)
{
TextBox txtItem = (row.FindControl("txt" + i) as TextBox);
dr[i] = txtItem.Text;

}
}
if (row.Cells[i].Controls[0].ToString() == "System.Web.UI.WebControls.DropDownList")
{
if (row.FindControl("ddl" + i) as DropDownList != null)
{
DropDownList txtItem = (row.FindControl("ddl" + i) as DropDownList);
dr[i] = txtItem.Text;
}
}
}
dt.Rows.Add(dr);
}
return dt;
}
protected void Delete(object sender, EventArgs e)
{
GetDataTable(gv);
dt.Rows.RemoveAt(Convert.ToInt16(selcrow.Value)); gv.DataSource = dt;
pbcount.Value = (Convert.ToInt16(pbcount.Value) - 1).ToString();
gv.DataBind();
}

protected void Add(object sender, EventArgs e)
{
GetDataTable(gv);
dt.Rows.Add("");
pbcount.Value = (Convert.ToInt16(pbcount .Value)+1).ToString();
gv.DataSource = dt;
gv.DataBind();
}
protected void Page_PreRender(object sender, EventArgs e)
{

}
protected void Page_Load(object sender, EventArgs e)
{
Createtemplatefields();
if (IsPostBack)
{
for (int i = 1; i < Convert.ToInt16(pbcount.Value); i++)
{
DataRow oItem = dt.NewRow();
oItem[0] = "";
dt.Rows.Add(oItem);
gv.DataSource = dt;
gv.DataBind();
}
}
}
public void Createtemplatefields()
{
for(int i=0;i<dt.Columns.Count;i++)
{
switch (dt.Rows[0][i].ToString().Substring(0, 3))
{
case "txt":
TemplateField tf1 = new TemplateField();
tf1.HeaderText = dt.Columns[i].ToString ();
gv.Columns.Add(tf1);
tf1.ItemTemplate = new TemplateHandler1("txt", i, dt.Columns[i].ToString());
break;
case "pic":
TemplateField tf2 = new TemplateField();
tf2.HeaderText = dt.Columns[i].ToString ();
gv.Columns.Add(tf2);
tf2.ItemTemplate = new TemplateHandler1("pic", i, dt.Columns[i].ToString());
break;
case "ddl":
TemplateField tf3 = new TemplateField();
tf3.HeaderText = dt.Columns[i].ToString();
string[] words = dt.Rows[0][i].ToString().Split('!');
gv.Columns.Add(tf3);
tf3.ItemTemplate = new TemplateHandler1("ddl", i, dt.Columns[i].ToString(),words);
break;
}

}
DataRow dr = dt.NewRow();
dt.Rows.Clear();
dr[0] = "";
dt.Rows.Add(dt);
gv.DataSource = dt;
gv.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "javascript:ChangeRowColor('" + e.Row.ClientID + "','" + e.Row.RowIndex + "')");

}
}

public DataTable Number
{
get
{
return this.dt;
}
set
{
this.dt = value;
}
}
}
}