473,748 Members | 7,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting the Click event of an TemplateField

Hi all,

I create a gridview dynamicly because I don't know the columns in advance.
I use the Templatefield to create a linkbutton.
Everything fine..I have the postbackurl and it works.
But now I need to do some extra stuff in the click event of the created
linkbutton.
I tried to assign a delegate to the linkbutton's click event but I never
gets in the procedure I assign.
How can I trap the click event?

Thank you.

Here is the code:

private void CreateGrid()
{
//Create the grid:

foreach (DataColumn col in dtt.Columns)

{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" ) ||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))

{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )

{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("Custom er"))

{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResult .Columns.Add(te mpColumn);

}

else

{

BoundField boundColumn = new BoundField();

boundColumn.Dat aField = col.ColumnName;

boundColumn.Hea derText = col.ColumnName;

grvSearchResult .Columns.Add(bo undColumn);

}

}

grvSearchResult .DataSource = dtt;

grvSearchResult .DataBind();
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r

{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg = "";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl, string
ColumnCommandAr g)

{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnComman dArg = ColumnCommandAr g;

}

public void InstantiateIn(C ontrol container)

{

LinkButton lnk = new LinkButton();

container.Contr ols.Add(lnk);

lnk.DataBinding += new EventHandler(ln k_DataBinding);

}

void lnk_DataBinding (object sender, EventArgs e)

{

LinkButton lnk = (LinkButton)sen der;

DataRowView drv = (DataRowView)(( GridViewRow)lnk .NamingContaine r).DataItem;

lnk.Text = drv[theColumnName].ToString();

lnk.PostBackUrl = thePostBackUrl + drv[theColumnComman dArg].ToString();

}

}

#endregion
Feb 23 '07 #1
6 2255
Please find fixed code below. Two things, 1. you have to create grid on init
event handler, 2. bind data only once (IsPostBack == false)

protected void Page_Init(objec t sender, EventArgs e)
{
CreateGrid();
}

public DataTable GetData()
{
DataTable t = new DataTable();

t.Columns.Add(" Id", typeof(int));
t.Columns.Add(" Name", typeof(string)) ;
t.Columns.Add(" Invoice", typeof(string)) ;
t.Columns.Add(" MarkID", typeof(int));

for (int i = 0; i < 10; i++)
{
string str = i.ToString();
DataRow r = t.NewRow();
r[0] = i;
r[1] = "name" + str;
r[2] = "inv" + str;
r[3] = i;
t.Rows.Add(r);
}

return t;
}

private void CreateGrid()
{
DataTable dtt = GetData();

foreach (DataColumn col in dtt.Columns)
{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" ) ||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("Custom er"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResult .Columns.Add(te mpColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.Dat aField = col.ColumnName;

boundColumn.Hea derText = col.ColumnName;

grvSearchResult .Columns.Add(bo undColumn);

}

}

if (!IsPostBack)
{
grvSearchResult .DataSource = dtt;
grvSearchResult .DataBind();
}
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r
{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg = "";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl, string
ColumnCommandAr g)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnComman dArg = ColumnCommandAr g;

}

public void InstantiateIn(C ontrol container)
{

LinkButton lnk = new LinkButton();

container.Contr ols.Add(lnk);

lnk.DataBinding += new EventHandler(ln k_DataBinding);
lnk.ID = "idx";
lnk.Click += new EventHandler(ln k_Click);

}

void lnk_Click(objec t sender, EventArgs e)
{
// event handler will execute now
}

void lnk_DataBinding (object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sen der;
DataRowView drv = (DataRowView)(( GridViewRow)lnk .NamingContaine r).DataItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnComman dArg].ToString();

}

}

#endregion
--
Milosz
"Class" wrote:
Hi all,

I create a gridview dynamicly because I don't know the columns in advance.
I use the Templatefield to create a linkbutton.
Everything fine..I have the postbackurl and it works.
But now I need to do some extra stuff in the click event of the created
linkbutton.
I tried to assign a delegate to the linkbutton's click event but I never
gets in the procedure I assign.
How can I trap the click event?

Thank you.

Here is the code:

private void CreateGrid()
{
//Create the grid:

foreach (DataColumn col in dtt.Columns)

{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" ) ||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))

{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )

{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("Custom er"))

{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResult .Columns.Add(te mpColumn);

}

else

{

BoundField boundColumn = new BoundField();

boundColumn.Dat aField = col.ColumnName;

boundColumn.Hea derText = col.ColumnName;

grvSearchResult .Columns.Add(bo undColumn);

}

}

grvSearchResult .DataSource = dtt;

grvSearchResult .DataBind();
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r

{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg = "";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl, string
ColumnCommandAr g)

{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnComman dArg = ColumnCommandAr g;

}

public void InstantiateIn(C ontrol container)

{

LinkButton lnk = new LinkButton();

container.Contr ols.Add(lnk);

lnk.DataBinding += new EventHandler(ln k_DataBinding);

}

void lnk_DataBinding (object sender, EventArgs e)

{

LinkButton lnk = (LinkButton)sen der;

DataRowView drv = (DataRowView)(( GridViewRow)lnk .NamingContaine r).DataItem;

lnk.Text = drv[theColumnName].ToString();

lnk.PostBackUrl = thePostBackUrl + drv[theColumnComman dArg].ToString();

}

}

#endregion
Feb 24 '07 #2
Hi Milosz,

great thanks for the help I will try it.
One more thing the click event will be in the Template class.
How do I get it to be in the Page?
I want to use the LinkButtonTempl ate class in more pages and grids.

Thank you.

"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plschr eef in bericht
news:B2******** *************** ***********@mic rosoft.com...
Please find fixed code below. Two things, 1. you have to create grid on
init
event handler, 2. bind data only once (IsPostBack == false)

protected void Page_Init(objec t sender, EventArgs e)
{
CreateGrid();
}

public DataTable GetData()
{
DataTable t = new DataTable();

t.Columns.Add(" Id", typeof(int));
t.Columns.Add(" Name", typeof(string)) ;
t.Columns.Add(" Invoice", typeof(string)) ;
t.Columns.Add(" MarkID", typeof(int));

for (int i = 0; i < 10; i++)
{
string str = i.ToString();
DataRow r = t.NewRow();
r[0] = i;
r[1] = "name" + str;
r[2] = "inv" + str;
r[3] = i;
t.Rows.Add(r);
}

return t;
}

private void CreateGrid()
{
DataTable dtt = GetData();

foreach (DataColumn col in dtt.Columns)
{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" ) ||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("Custom er"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResult .Columns.Add(te mpColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.Dat aField = col.ColumnName;

boundColumn.Hea derText = col.ColumnName;

grvSearchResult .Columns.Add(bo undColumn);

}

}

if (!IsPostBack)
{
grvSearchResult .DataSource = dtt;
grvSearchResult .DataBind();
}
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r
{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg = "";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl, string
ColumnCommandAr g)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnComman dArg = ColumnCommandAr g;

}

public void InstantiateIn(C ontrol container)
{

LinkButton lnk = new LinkButton();

container.Contr ols.Add(lnk);

lnk.DataBinding += new EventHandler(ln k_DataBinding);
lnk.ID = "idx";
lnk.Click += new EventHandler(ln k_Click);

}

void lnk_Click(objec t sender, EventArgs e)
{
// event handler will execute now
}

void lnk_DataBinding (object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sen der;
DataRowView drv =
(DataRowView)(( GridViewRow)lnk .NamingContaine r).DataItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnComman dArg].ToString();

}

}

#endregion
--
Milosz
"Class" wrote:
>Hi all,

I create a gridview dynamicly because I don't know the columns in
advance.
I use the Templatefield to create a linkbutton.
Everything fine..I have the postbackurl and it works.
But now I need to do some extra stuff in the click event of the created
linkbutton.
I tried to assign a delegate to the linkbutton's click event but I never
gets in the procedure I assign.
How can I trap the click event?

Thank you.

Here is the code:

private void CreateGrid()
{
//Create the grid:

foreach (DataColumn col in dtt.Columns)

{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" ) ||
col.ColumnName .Equals("FilID" ))

continue;

if (col.ColumnName .Equals("Invoic e"))

{

TemplateFiel d tempColumn = new TemplateField() ;

tempColumn.Hea derText = col.ColumnName;

tempColumn.Ite mTemplate = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

grvSearchResul t.Columns.Add(t empColumn);

}

else if (col.ColumnName .Equals("File") )

{

TemplateFiel d tempColumn = new TemplateField() ;

tempColumn.Hea derText = col.ColumnName;

tempColumn.Ite mTemplate = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResul t.Columns.Add(t empColumn);

}

else if (col.ColumnName .Equals("Custom er"))

{

TemplateFiel d tempColumn = new TemplateField() ;

tempColumn.Hea derText = col.ColumnName;

tempColumn.Ite mTemplate = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResul t.Columns.Add(t empColumn);

}

else

{

BoundField boundColumn = new BoundField();

boundColumn.Da taField = col.ColumnName;

boundColumn.He aderText = col.ColumnName;

grvSearchResul t.Columns.Add(b oundColumn);

}

}

grvSearchResul t.DataSource = dtt;

grvSearchResul t.DataBind();
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r

{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg = "";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl, string
ColumnCommandA rg)

{

theColumnNam e = ColumnName;

thePostBackU rl = PostBackUrl;

theColumnComma ndArg = ColumnCommandAr g;

}

public void InstantiateIn(C ontrol container)

{

LinkButton lnk = new LinkButton();

container.Cont rols.Add(lnk);

lnk.DataBindin g += new EventHandler(ln k_DataBinding);

}

void lnk_DataBinding (object sender, EventArgs e)

{

LinkButton lnk = (LinkButton)sen der;

DataRowView drv =
(DataRowView)( (GridViewRow)ln k.NamingContain er).DataItem;

lnk.Text = drv[theColumnName].ToString();

lnk.PostBackUr l = thePostBackUrl + drv[theColumnComman dArg].ToString();

}

}

#endregion

Feb 24 '07 #3
Please find simple resolution below. Note you'll have to write the same code
on every page anyway, so wouldn't just create a user control and expose some
properties to make it generic? (if you don't know how to approach that let me
know)

using System;
using System.Data;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Text.Reg ularExpressions ;

public partial class _Default : BasePage
{
protected void Page_Init(objec t sender, EventArgs e)
{
CreateGrid();
}

public DataTable GetData()
{
DataTable t = new DataTable();

t.Columns.Add(" Id", typeof(int));
t.Columns.Add(" Name", typeof(string)) ;
t.Columns.Add(" Invoice", typeof(string)) ;
t.Columns.Add(" MarkID", typeof(int));

for (int i = 0; i < 10; i++)
{
string str = i.ToString();
DataRow r = t.NewRow();
r[0] = i;
r[1] = "name" + str;
r[2] = "inv" + str;
r[3] = i;
t.Rows.Add(r);
}

return t;
}

private void CreateGrid()
{
DataTable dtt = GetData();

foreach (DataColumn col in dtt.Columns)
{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" ) ||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

LinkButtonTempl ate template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

template.LinkBu ttonClick += new EventHandler(te mplate_LinkButt onClick);

tempColumn.Item Template = template;

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("Custom er"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResult .Columns.Add(te mpColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.Dat aField = col.ColumnName;

boundColumn.Hea derText = col.ColumnName;

grvSearchResult .Columns.Add(bo undColumn);

}

}

if (!IsPostBack)
{
grvSearchResult .DataSource = dtt;
grvSearchResult .DataBind();
}
}

void template_LinkBu ttonClick(objec t sender, EventArgs e)
{
//whatever
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r
{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg = "";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl, string
ColumnCommandAr g)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnComman dArg = ColumnCommandAr g;

}

public event EventHandler LinkButtonClick ;

public void InstantiateIn(C ontrol container)
{

LinkButton lnk = new LinkButton();

container.Contr ols.Add(lnk);

lnk.DataBinding += new EventHandler(ln k_DataBinding);
lnk.ID = "idx";

if (LinkButtonClic k != null)
lnk.Click += new EventHandler(Li nkButtonClick);

}

void lnk_Click(objec t sender, EventArgs e)
{
// event handler will execute now
}

void lnk_DataBinding (object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sen der;
DataRowView drv = (DataRowView)(( GridViewRow)lnk .NamingContaine r).DataItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnComman dArg].ToString();

}

}

#endregion

}

--
Milosz
"Class" wrote:
Hi Milosz,

great thanks for the help I will try it.
One more thing the click event will be in the Template class.
How do I get it to be in the Page?
I want to use the LinkButtonTempl ate class in more pages and grids.

Thank you.

"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plschr eef in bericht
news:B2******** *************** ***********@mic rosoft.com...
Please find fixed code below. Two things, 1. you have to create grid on
init
event handler, 2. bind data only once (IsPostBack == false)

protected void Page_Init(objec t sender, EventArgs e)
{
CreateGrid();
}

public DataTable GetData()
{
DataTable t = new DataTable();

t.Columns.Add(" Id", typeof(int));
t.Columns.Add(" Name", typeof(string)) ;
t.Columns.Add(" Invoice", typeof(string)) ;
t.Columns.Add(" MarkID", typeof(int));

for (int i = 0; i < 10; i++)
{
string str = i.ToString();
DataRow r = t.NewRow();
r[0] = i;
r[1] = "name" + str;
r[2] = "inv" + str;
r[3] = i;
t.Rows.Add(r);
}

return t;
}

private void CreateGrid()
{
DataTable dtt = GetData();

foreach (DataColumn col in dtt.Columns)
{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" ) ||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("Custom er"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResult .Columns.Add(te mpColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.Dat aField = col.ColumnName;

boundColumn.Hea derText = col.ColumnName;

grvSearchResult .Columns.Add(bo undColumn);

}

}

if (!IsPostBack)
{
grvSearchResult .DataSource = dtt;
grvSearchResult .DataBind();
}
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r
{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg = "";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl, string
ColumnCommandAr g)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnComman dArg = ColumnCommandAr g;

}

public void InstantiateIn(C ontrol container)
{

LinkButton lnk = new LinkButton();

container.Contr ols.Add(lnk);

lnk.DataBinding += new EventHandler(ln k_DataBinding);
lnk.ID = "idx";
lnk.Click += new EventHandler(ln k_Click);

}

void lnk_Click(objec t sender, EventArgs e)
{
// event handler will execute now
}

void lnk_DataBinding (object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sen der;
DataRowView drv =
(DataRowView)(( GridViewRow)lnk .NamingContaine r).DataItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnComman dArg].ToString();

}

}

#endregion
--
Milosz
"Class" wrote:
Hi all,

I create a gridview dynamicly because I don't know the columns in
advance.
I use the Templatefield to create a linkbutton.
Everything fine..I have the postbackurl and it works.
But now I need to do some extra stuff in the click event of the created
linkbutton.
I tried to assign a delegate to the linkbutton's click event but I never
gets in the procedure I assign.
How can I trap the click event?

Thank you.

Here is the code:

private void CreateGrid()
{
//Create the grid:

foreach (DataColumn col in dtt.Columns)

{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" ) ||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))

{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )

{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("Custom er"))

{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResult .Columns.Add(te mpColumn);

}

else

{

BoundField boundColumn = new BoundField();

boundColumn.Dat aField = col.ColumnName;

boundColumn.Hea derText = col.ColumnName;

grvSearchResult .Columns.Add(bo undColumn);

}

}

grvSearchResult .DataSource = dtt;

grvSearchResult .DataBind();
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r

{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg = "";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl, string
ColumnCommandAr g)

{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnComman dArg = ColumnCommandAr g;

}

public void InstantiateIn(C ontrol container)

{
Feb 24 '07 #4
Hi Milosz,

WOW! great help thank you alot!
I understand what you mean with the user control way.

One more thing. This is on a search page and after clicking the search
button I know what to search for.
Then I create the grid.
But that's after the Page_Init and Page_Load.
Is there something I can do about that?

Thanks again.

"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plschr eef in bericht
news:8A******** *************** ***********@mic rosoft.com...
Please find simple resolution below. Note you'll have to write the same
code
on every page anyway, so wouldn't just create a user control and expose
some
properties to make it generic? (if you don't know how to approach that let
me
know)

using System;
using System.Data;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Text.Reg ularExpressions ;

public partial class _Default : BasePage
{
protected void Page_Init(objec t sender, EventArgs e)
{
CreateGrid();
}

public DataTable GetData()
{
DataTable t = new DataTable();

t.Columns.Add(" Id", typeof(int));
t.Columns.Add(" Name", typeof(string)) ;
t.Columns.Add(" Invoice", typeof(string)) ;
t.Columns.Add(" MarkID", typeof(int));

for (int i = 0; i < 10; i++)
{
string str = i.ToString();
DataRow r = t.NewRow();
r[0] = i;
r[1] = "name" + str;
r[2] = "inv" + str;
r[3] = i;
t.Rows.Add(r);
}

return t;
}

private void CreateGrid()
{
DataTable dtt = GetData();

foreach (DataColumn col in dtt.Columns)
{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" ) ||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

LinkButtonTempl ate template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

template.LinkBu ttonClick += new EventHandler(te mplate_LinkButt onClick);

tempColumn.Item Template = template;

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("Custom er"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResult .Columns.Add(te mpColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.Dat aField = col.ColumnName;

boundColumn.Hea derText = col.ColumnName;

grvSearchResult .Columns.Add(bo undColumn);

}

}

if (!IsPostBack)
{
grvSearchResult .DataSource = dtt;
grvSearchResult .DataBind();
}
}

void template_LinkBu ttonClick(objec t sender, EventArgs e)
{
//whatever
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r
{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg = "";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl, string
ColumnCommandAr g)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnComman dArg = ColumnCommandAr g;

}

public event EventHandler LinkButtonClick ;

public void InstantiateIn(C ontrol container)
{

LinkButton lnk = new LinkButton();

container.Contr ols.Add(lnk);

lnk.DataBinding += new EventHandler(ln k_DataBinding);
lnk.ID = "idx";

if (LinkButtonClic k != null)
lnk.Click += new EventHandler(Li nkButtonClick);

}

void lnk_Click(objec t sender, EventArgs e)
{
// event handler will execute now
}

void lnk_DataBinding (object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sen der;
DataRowView drv =
(DataRowView)(( GridViewRow)lnk .NamingContaine r).DataItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnComman dArg].ToString();

}

}

#endregion

}

--
Milosz
"Class" wrote:
>Hi Milosz,

great thanks for the help I will try it.
One more thing the click event will be in the Template class.
How do I get it to be in the Page?
I want to use the LinkButtonTempl ate class in more pages and grids.

Thank you.

"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plschr eef in bericht
news:B2******* *************** ************@mi crosoft.com...
Please find fixed code below. Two things, 1. you have to create grid on
init
event handler, 2. bind data only once (IsPostBack == false)

protected void Page_Init(objec t sender, EventArgs e)
{
CreateGrid();
}

public DataTable GetData()
{
DataTable t = new DataTable();

t.Columns.Add(" Id", typeof(int));
t.Columns.Add(" Name", typeof(string)) ;
t.Columns.Add(" Invoice", typeof(string)) ;
t.Columns.Add(" MarkID", typeof(int));

for (int i = 0; i < 10; i++)
{
string str = i.ToString();
DataRow r = t.NewRow();
r[0] = i;
r[1] = "name" + str;
r[2] = "inv" + str;
r[3] = i;
t.Rows.Add(r);
}

return t;
}

private void CreateGrid()
{
DataTable dtt = GetData();

foreach (DataColumn col in dtt.Columns)
{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" )
||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("Custom er"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResult .Columns.Add(te mpColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.Dat aField = col.ColumnName;

boundColumn.Hea derText = col.ColumnName;

grvSearchResult .Columns.Add(bo undColumn);

}

}

if (!IsPostBack)
{
grvSearchResult .DataSource = dtt;
grvSearchResult .DataBind();
}
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r
{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg =
"";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl, string
ColumnCommandAr g)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnComman dArg = ColumnCommandAr g;

}

public void InstantiateIn(C ontrol container)
{

LinkButton lnk = new LinkButton();

container.Contr ols.Add(lnk);

lnk.DataBinding += new EventHandler(ln k_DataBinding);
lnk.ID = "idx";
lnk.Click += new EventHandler(ln k_Click);

}

void lnk_Click(objec t sender, EventArgs e)
{
// event handler will execute now
}

void lnk_DataBinding (object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sen der;
DataRowView drv =
(DataRowView)(( GridViewRow)lnk .NamingContaine r).DataItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl +
drv[theColumnComman dArg].ToString();

}

}

#endregion
--
Milosz
"Class" wrote:

Hi all,

I create a gridview dynamicly because I don't know the columns in
advance.
I use the Templatefield to create a linkbutton.
Everything fine..I have the postbackurl and it works.
But now I need to do some extra stuff in the click event of the
created
linkbutton.
I tried to assign a delegate to the linkbutton's click event but I
never
gets in the procedure I assign.
How can I trap the click event?

Thank you.

Here is the code:

private void CreateGrid()
{
//Create the grid:

foreach (DataColumn col in dtt.Columns)

{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" )
||
col.ColumnName .Equals("FilID" ))

continue;

if (col.ColumnName .Equals("Invoic e"))

{

TemplateFiel d tempColumn = new TemplateField() ;

tempColumn.Hea derText = col.ColumnName;

tempColumn.Ite mTemplate = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

grvSearchResul t.Columns.Add(t empColumn);

}

else if (col.ColumnName .Equals("File") )

{

TemplateFiel d tempColumn = new TemplateField() ;

tempColumn.Hea derText = col.ColumnName;

tempColumn.Ite mTemplate = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResul t.Columns.Add(t empColumn);

}

else if (col.ColumnName .Equals("Custom er"))

{

TemplateFiel d tempColumn = new TemplateField() ;

tempColumn.Hea derText = col.ColumnName;

tempColumn.Ite mTemplate = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResul t.Columns.Add(t empColumn);

}

else

{

BoundField boundColumn = new BoundField();

boundColumn.Da taField = col.ColumnName;

boundColumn.He aderText = col.ColumnName;

grvSearchResul t.Columns.Add(b oundColumn);

}

}

grvSearchResul t.DataSource = dtt;

grvSearchResul t.DataBind();
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r

{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg =
"";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl,
string
ColumnCommandA rg)

{

theColumnNam e = ColumnName;

thePostBackU rl = PostBackUrl;

theColumnComma ndArg = ColumnCommandAr g;

}

public void InstantiateIn(C ontrol container)

{

Feb 24 '07 #5
You have to recreate the grid because criteria have changed:

protected void SearchButton_Cl ick(object sender, EventArgs e)
{
string criteria = txtCrieria.Text ;
// get data source based on carent criteria
CreateGrid();
}
--
Milosz
"Class" wrote:
Hi Milosz,

WOW! great help thank you alot!
I understand what you mean with the user control way.

One more thing. This is on a search page and after clicking the search
button I know what to search for.
Then I create the grid.
But that's after the Page_Init and Page_Load.
Is there something I can do about that?

Thanks again.

"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plschr eef in bericht
news:8A******** *************** ***********@mic rosoft.com...
Please find simple resolution below. Note you'll have to write the same
code
on every page anyway, so wouldn't just create a user control and expose
some
properties to make it generic? (if you don't know how to approach that let
me
know)

using System;
using System.Data;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Text.Reg ularExpressions ;

public partial class _Default : BasePage
{
protected void Page_Init(objec t sender, EventArgs e)
{
CreateGrid();
}

public DataTable GetData()
{
DataTable t = new DataTable();

t.Columns.Add(" Id", typeof(int));
t.Columns.Add(" Name", typeof(string)) ;
t.Columns.Add(" Invoice", typeof(string)) ;
t.Columns.Add(" MarkID", typeof(int));

for (int i = 0; i < 10; i++)
{
string str = i.ToString();
DataRow r = t.NewRow();
r[0] = i;
r[1] = "name" + str;
r[2] = "inv" + str;
r[3] = i;
t.Rows.Add(r);
}

return t;
}

private void CreateGrid()
{
DataTable dtt = GetData();

foreach (DataColumn col in dtt.Columns)
{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" ) ||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

LinkButtonTempl ate template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

template.LinkBu ttonClick += new EventHandler(te mplate_LinkButt onClick);

tempColumn.Item Template = template;

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("Custom er"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResult .Columns.Add(te mpColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.Dat aField = col.ColumnName;

boundColumn.Hea derText = col.ColumnName;

grvSearchResult .Columns.Add(bo undColumn);

}

}

if (!IsPostBack)
{
grvSearchResult .DataSource = dtt;
grvSearchResult .DataBind();
}
}

void template_LinkBu ttonClick(objec t sender, EventArgs e)
{
//whatever
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r
{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg = "";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl, string
ColumnCommandAr g)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnComman dArg = ColumnCommandAr g;

}

public event EventHandler LinkButtonClick ;

public void InstantiateIn(C ontrol container)
{

LinkButton lnk = new LinkButton();

container.Contr ols.Add(lnk);

lnk.DataBinding += new EventHandler(ln k_DataBinding);
lnk.ID = "idx";

if (LinkButtonClic k != null)
lnk.Click += new EventHandler(Li nkButtonClick);

}

void lnk_Click(objec t sender, EventArgs e)
{
// event handler will execute now
}

void lnk_DataBinding (object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sen der;
DataRowView drv =
(DataRowView)(( GridViewRow)lnk .NamingContaine r).DataItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnComman dArg].ToString();

}

}

#endregion

}

--
Milosz
"Class" wrote:
Hi Milosz,

great thanks for the help I will try it.
One more thing the click event will be in the Template class.
How do I get it to be in the Page?
I want to use the LinkButtonTempl ate class in more pages and grids.

Thank you.

"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plschr eef in bericht
news:B2******** *************** ***********@mic rosoft.com...
Please find fixed code below. Two things, 1. you have to create grid on
init
event handler, 2. bind data only once (IsPostBack == false)

protected void Page_Init(objec t sender, EventArgs e)
{
CreateGrid();
}

public DataTable GetData()
{
DataTable t = new DataTable();

t.Columns.Add(" Id", typeof(int));
t.Columns.Add(" Name", typeof(string)) ;
t.Columns.Add(" Invoice", typeof(string)) ;
t.Columns.Add(" MarkID", typeof(int));

for (int i = 0; i < 10; i++)
{
string str = i.ToString();
DataRow r = t.NewRow();
r[0] = i;
r[1] = "name" + str;
r[2] = "inv" + str;
r[3] = i;
t.Rows.Add(r);
}

return t;
}

private void CreateGrid()
{
DataTable dtt = GetData();

foreach (DataColumn col in dtt.Columns)
{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" )
||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);
Feb 24 '07 #6
Thanks alot Milosz!!!!!!!!!
"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plschr eef in bericht
news:2D******** *************** ***********@mic rosoft.com...
You have to recreate the grid because criteria have changed:

protected void SearchButton_Cl ick(object sender, EventArgs e)
{
string criteria = txtCrieria.Text ;
// get data source based on carent criteria
CreateGrid();
}
--
Milosz
"Class" wrote:
>Hi Milosz,

WOW! great help thank you alot!
I understand what you mean with the user control way.

One more thing. This is on a search page and after clicking the search
button I know what to search for.
Then I create the grid.
But that's after the Page_Init and Page_Load.
Is there something I can do about that?

Thanks again.

"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plschr eef in bericht
news:8A******* *************** ************@mi crosoft.com...
Please find simple resolution below. Note you'll have to write the same
code
on every page anyway, so wouldn't just create a user control and expose
some
properties to make it generic? (if you don't know how to approach that
let
me
know)

using System;
using System.Data;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Text.Reg ularExpressions ;

public partial class _Default : BasePage
{
protected void Page_Init(objec t sender, EventArgs e)
{
CreateGrid();
}

public DataTable GetData()
{
DataTable t = new DataTable();

t.Columns.Add(" Id", typeof(int));
t.Columns.Add(" Name", typeof(string)) ;
t.Columns.Add(" Invoice", typeof(string)) ;
t.Columns.Add(" MarkID", typeof(int));

for (int i = 0; i < 10; i++)
{
string str = i.ToString();
DataRow r = t.NewRow();
r[0] = i;
r[1] = "name" + str;
r[2] = "inv" + str;
r[3] = i;
t.Rows.Add(r);
}

return t;
}

private void CreateGrid()
{
DataTable dtt = GetData();

foreach (DataColumn col in dtt.Columns)
{

if (col.ColumnName .Equals("FakAID ") || col.ColumnName. Equals("markID" )
||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

LinkButtonTempl ate template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

template.LinkBu ttonClick += new EventHandler(te mplate_LinkButt onClick);

tempColumn.Item Template = template;

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("Custom er"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screent.aspx?na v=1&cid=", "FakID");

grvSearchResult .Columns.Add(te mpColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.Dat aField = col.ColumnName;

boundColumn.Hea derText = col.ColumnName;

grvSearchResult .Columns.Add(bo undColumn);

}

}

if (!IsPostBack)
{
grvSearchResult .DataSource = dtt;
grvSearchResult .DataBind();
}
}

void template_LinkBu ttonClick(objec t sender, EventArgs e)
{
//whatever
}
#region LinkButtonTempl ate

public class LinkButtonTempl ate : ITemplate, INamingContaine r
{

string theColumnName = "", thePostBackUrl = "", theColumnComman dArg =
"";

public LinkButtonTempl ate(string ColumnName, string PostBackUrl, string
ColumnCommandAr g)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnComman dArg = ColumnCommandAr g;

}

public event EventHandler LinkButtonClick ;

public void InstantiateIn(C ontrol container)
{

LinkButton lnk = new LinkButton();

container.Contr ols.Add(lnk);

lnk.DataBinding += new EventHandler(ln k_DataBinding);
lnk.ID = "idx";

if (LinkButtonClic k != null)
lnk.Click += new EventHandler(Li nkButtonClick);

}

void lnk_Click(objec t sender, EventArgs e)
{
// event handler will execute now
}

void lnk_DataBinding (object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sen der;
DataRowView drv =
(DataRowView)(( GridViewRow)lnk .NamingContaine r).DataItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl +
drv[theColumnComman dArg].ToString();

}

}

#endregion

}

--
Milosz
"Class" wrote:

Hi Milosz,

great thanks for the help I will try it.
One more thing the click event will be in the Template class.
How do I get it to be in the Page?
I want to use the LinkButtonTempl ate class in more pages and grids.

Thank you.

"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plschr eef in
bericht
news:B2******* *************** ************@mi crosoft.com...
Please find fixed code below. Two things, 1. you have to create grid
on
init
event handler, 2. bind data only once (IsPostBack == false)

protected void Page_Init(objec t sender, EventArgs e)
{
CreateGrid();
}

public DataTable GetData()
{
DataTable t = new DataTable();

t.Columns.Add(" Id", typeof(int));
t.Columns.Add(" Name", typeof(string)) ;
t.Columns.Add(" Invoice", typeof(string)) ;
t.Columns.Add(" MarkID", typeof(int));

for (int i = 0; i < 10; i++)
{
string str = i.ToString();
DataRow r = t.NewRow();
r[0] = i;
r[1] = "name" + str;
r[2] = "inv" + str;
r[3] = i;
t.Rows.Add(r);
}

return t;
}

private void CreateGrid()
{
DataTable dtt = GetData();

foreach (DataColumn col in dtt.Columns)
{

if (col.ColumnName .Equals("FakAID ") ||
col.ColumnName. Equals("markID" )
||
col.ColumnName. Equals("FilID") )

continue;

if (col.ColumnName .Equals("Invoic e"))
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&tab=I&cid=" , "markID");

grvSearchResult .Columns.Add(te mpColumn);

}

else if (col.ColumnName .Equals("File") )
{

TemplateField tempColumn = new TemplateField() ;

tempColumn.Head erText = col.ColumnName;

tempColumn.Item Template = new LinkButtonTempl ate(col.ColumnN ame,
"~/screen.aspx?nav =1&fid=", "FilID");

grvSearchResult .Columns.Add(te mpColumn);

Feb 25 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2274
by: samuelberthelot | last post by:
Hi, Hi have a hyperlink column in my datagridview. How can I capture the click event on the hyperlink ? Thank you
1
10443
by: Nam | last post by:
I have a DropDownList control in a TemplateField inside my GridView How do I access this control’s SelectedIndexChanged event in codeBehind page? I don’t see the ID “DropDownList1” of this control in the list of objects in odeBehind page. GridView1 and DropDownList1 are bound to TWO DIFFERENT tables (DataSources). The last column of each row in the GridView is shown as a DropDownList. I want to use the SelectedIndexChanged ...
0
1747
by: =?Utf-8?B?cGI2NDgxNzQ=?= | last post by:
We have been having this problem for months and always assumed it was because of our somewhat complicated setup. I have just spent almost all of today making this into a simple example that can be easily reproduced and it seems like a major .NET flaw/limitation that I was hoping you could explain to me as it is really frustrating me. I just want to be able to get info for a row clicked, whether it is a Repeater, GridView, etc. I have...
7
17317
by: =?Utf-8?B?cGF0cmlja2RyZA==?= | last post by:
Hi all! I have a gridview inside a datagrid (nested) for which (gridview) the rowcommand is not raised in order to delete a row from the grid! I also tried OnRowCommand="method", didn't work either! Does anyone know how can I make this work? Thanks in advance!
0
4927
by: David Lozzi | last post by:
Howdy, I have a gridview with a linkbutton in a template column (code is below). When the linkbutton is clicked i want to display a little confirm right in the gridview to confirm deleting the row. I tried using the following to grab the index, which appears to work on command columns, but not with buttons in a template? is this true? or am i missing something. how do I get the row index? Its returning the ID of the record, not the index...
4
7221
by: SEliel | last post by:
Hello everyone: I'm programming a custom GridView, adding column by column dynamically. Every column is a TemplateField, and I've made a class hierarchy for each template (TextColumnTemplate, DropDownListColumnTemplate, ButtonColumnTemplate), implementing ITemplate. The problem is in ButtonColumnTemplate. Here's is my code:
0
2147
by: baburk | last post by:
I am having dropdownlist inside gridview. When the dropdownlist selectedindex change an event should fire. What is the event for his. I also want to get the dropdownlist event fired row number <asp:GridView ID = "gvMaterials" runat = "server" AutoGenerateColumns = "false"> <Columns>
0
1069
by: rajesh0303 | last post by:
Iam get null values in the datakeynames AlertID,ItemID while executing the Insert Command....I am Inserting using Externel button Event.Here is the code of Gridiew ,Its datasource and the button event......Need help.... <asp:GridView ID="GridView2" DataSourceID="SqlDataSource2" DataKeyNames="AlertID,ItemID" AutoGenerateColumns="False" runat="server" Width="839px" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True"> ...
4
3729
by: SAL | last post by:
Hello, I'm working, basically my first, AJAX page and am having a few problems. One is that the Click event for a button I have in UpdatePanel1 is not getting called. I've tried with the button both inside and outside of the updatepanel and the event doesn't get called either way. What might I be missing here? Incidently, something else, kind of weird, is happening when the button is clicked, a required field validator control in a...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.