By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

Getting the Click event of an TemplateField

P: n/a
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("Invoice"))

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

grvSearchResult.Columns.Add(tempColumn);

}

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

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else

{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

grvSearchResult.DataSource = dtt;

grvSearchResult.DataBind();
}
#region LinkButtonTemplate

public class LinkButtonTemplate : ITemplate, INamingContainer

{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg = "";

public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)

{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public void InstantiateIn(Control container)

{

LinkButton lnk = new LinkButton();

container.Controls.Add(lnk);

lnk.DataBinding += new EventHandler(lnk_DataBinding);

}

void lnk_DataBinding(object sender, EventArgs e)

{

LinkButton lnk = (LinkButton)sender;

DataRowView drv = (DataRowView)((GridViewRow)lnk.NamingContainer).Da taItem;

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

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

}

}

#endregion
Feb 23 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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(object 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("Invoice"))
{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

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

public class LinkButtonTemplate : ITemplate, INamingContainer
{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg = "";

public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public void InstantiateIn(Control container)
{

LinkButton lnk = new LinkButton();

container.Controls.Add(lnk);

lnk.DataBinding += new EventHandler(lnk_DataBinding);
lnk.ID = "idx";
lnk.Click += new EventHandler(lnk_Click);

}

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

void lnk_DataBinding(object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sender;
DataRowView drv = (DataRowView)((GridViewRow)lnk.NamingContainer).Da taItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnCommandArg].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("Invoice"))

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

grvSearchResult.Columns.Add(tempColumn);

}

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

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else

{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

grvSearchResult.DataSource = dtt;

grvSearchResult.DataBind();
}
#region LinkButtonTemplate

public class LinkButtonTemplate : ITemplate, INamingContainer

{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg = "";

public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)

{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public void InstantiateIn(Control container)

{

LinkButton lnk = new LinkButton();

container.Controls.Add(lnk);

lnk.DataBinding += new EventHandler(lnk_DataBinding);

}

void lnk_DataBinding(object sender, EventArgs e)

{

LinkButton lnk = (LinkButton)sender;

DataRowView drv = (DataRowView)((GridViewRow)lnk.NamingContainer).Da taItem;

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

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

}

}

#endregion
Feb 24 '07 #2

P: n/a
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 LinkButtonTemplate class in more pages and grids.

Thank you.

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plschreef in bericht
news:B2**********************************@microsof t.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(object 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("Invoice"))
{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

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

public class LinkButtonTemplate : ITemplate, INamingContainer
{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg = "";

public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public void InstantiateIn(Control container)
{

LinkButton lnk = new LinkButton();

container.Controls.Add(lnk);

lnk.DataBinding += new EventHandler(lnk_DataBinding);
lnk.ID = "idx";
lnk.Click += new EventHandler(lnk_Click);

}

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

void lnk_DataBinding(object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sender;
DataRowView drv =
(DataRowView)((GridViewRow)lnk.NamingContainer).Da taItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnCommandArg].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("Invoice"))

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

grvSearchResult.Columns.Add(tempColumn);

}

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

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else

{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

grvSearchResult.DataSource = dtt;

grvSearchResult.DataBind();
}
#region LinkButtonTemplate

public class LinkButtonTemplate : ITemplate, INamingContainer

{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg = "";

public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)

{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public void InstantiateIn(Control container)

{

LinkButton lnk = new LinkButton();

container.Controls.Add(lnk);

lnk.DataBinding += new EventHandler(lnk_DataBinding);

}

void lnk_DataBinding(object sender, EventArgs e)

{

LinkButton lnk = (LinkButton)sender;

DataRowView drv =
(DataRowView)((GridViewRow)lnk.NamingContainer).D ataItem;

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

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

}

}

#endregion

Feb 24 '07 #3

P: n/a
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.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;
using System.Text.RegularExpressions;

public partial class _Default : BasePage
{
protected void Page_Init(object 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("Invoice"))
{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

LinkButtonTemplate template = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

template.LinkButtonClick += new EventHandler(template_LinkButtonClick);

tempColumn.ItemTemplate = template;

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

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

void template_LinkButtonClick(object sender, EventArgs e)
{
//whatever
}
#region LinkButtonTemplate

public class LinkButtonTemplate : ITemplate, INamingContainer
{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg = "";

public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public event EventHandler LinkButtonClick;

public void InstantiateIn(Control container)
{

LinkButton lnk = new LinkButton();

container.Controls.Add(lnk);

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

if (LinkButtonClick != null)
lnk.Click += new EventHandler(LinkButtonClick);

}

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

void lnk_DataBinding(object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sender;
DataRowView drv = (DataRowView)((GridViewRow)lnk.NamingContainer).Da taItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnCommandArg].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 LinkButtonTemplate class in more pages and grids.

Thank you.

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plschreef in bericht
news:B2**********************************@microsof t.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(object 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("Invoice"))
{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

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

public class LinkButtonTemplate : ITemplate, INamingContainer
{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg = "";

public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public void InstantiateIn(Control container)
{

LinkButton lnk = new LinkButton();

container.Controls.Add(lnk);

lnk.DataBinding += new EventHandler(lnk_DataBinding);
lnk.ID = "idx";
lnk.Click += new EventHandler(lnk_Click);

}

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

void lnk_DataBinding(object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sender;
DataRowView drv =
(DataRowView)((GridViewRow)lnk.NamingContainer).Da taItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnCommandArg].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("Invoice"))

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

grvSearchResult.Columns.Add(tempColumn);

}

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

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else

{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

grvSearchResult.DataSource = dtt;

grvSearchResult.DataBind();
}
#region LinkButtonTemplate

public class LinkButtonTemplate : ITemplate, INamingContainer

{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg = "";

public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)

{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public void InstantiateIn(Control container)

{
Feb 24 '07 #4

P: n/a
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*****@DONTLIKESPAMwp.plschreef in bericht
news:8A**********************************@microsof t.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.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;
using System.Text.RegularExpressions;

public partial class _Default : BasePage
{
protected void Page_Init(object 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("Invoice"))
{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

LinkButtonTemplate template = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

template.LinkButtonClick += new EventHandler(template_LinkButtonClick);

tempColumn.ItemTemplate = template;

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

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

void template_LinkButtonClick(object sender, EventArgs e)
{
//whatever
}
#region LinkButtonTemplate

public class LinkButtonTemplate : ITemplate, INamingContainer
{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg = "";

public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public event EventHandler LinkButtonClick;

public void InstantiateIn(Control container)
{

LinkButton lnk = new LinkButton();

container.Controls.Add(lnk);

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

if (LinkButtonClick != null)
lnk.Click += new EventHandler(LinkButtonClick);

}

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

void lnk_DataBinding(object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sender;
DataRowView drv =
(DataRowView)((GridViewRow)lnk.NamingContainer).Da taItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnCommandArg].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 LinkButtonTemplate class in more pages and grids.

Thank you.

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plschreef in bericht
news:B2**********************************@microso ft.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(object 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("Invoice"))
{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

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

public class LinkButtonTemplate : ITemplate, INamingContainer
{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg =
"";

public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public void InstantiateIn(Control container)
{

LinkButton lnk = new LinkButton();

container.Controls.Add(lnk);

lnk.DataBinding += new EventHandler(lnk_DataBinding);
lnk.ID = "idx";
lnk.Click += new EventHandler(lnk_Click);

}

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

void lnk_DataBinding(object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sender;
DataRowView drv =
(DataRowView)((GridViewRow)lnk.NamingContainer).Da taItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl +
drv[theColumnCommandArg].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("Invoice"))

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

grvSearchResult.Columns.Add(tempColumn);

}

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

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else

{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

grvSearchResult.DataSource = dtt;

grvSearchResult.DataBind();
}
#region LinkButtonTemplate

public class LinkButtonTemplate : ITemplate, INamingContainer

{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg =
"";

public LinkButtonTemplate(string ColumnName, string PostBackUrl,
string
ColumnCommandArg)

{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public void InstantiateIn(Control container)

{

Feb 24 '07 #5

P: n/a
You have to recreate the grid because criteria have changed:

protected void SearchButton_Click(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*****@DONTLIKESPAMwp.plschreef in bericht
news:8A**********************************@microsof t.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.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;
using System.Text.RegularExpressions;

public partial class _Default : BasePage
{
protected void Page_Init(object 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("Invoice"))
{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

LinkButtonTemplate template = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

template.LinkButtonClick += new EventHandler(template_LinkButtonClick);

tempColumn.ItemTemplate = template;

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

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

void template_LinkButtonClick(object sender, EventArgs e)
{
//whatever
}
#region LinkButtonTemplate

public class LinkButtonTemplate : ITemplate, INamingContainer
{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg = "";

public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public event EventHandler LinkButtonClick;

public void InstantiateIn(Control container)
{

LinkButton lnk = new LinkButton();

container.Controls.Add(lnk);

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

if (LinkButtonClick != null)
lnk.Click += new EventHandler(LinkButtonClick);

}

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

void lnk_DataBinding(object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sender;
DataRowView drv =
(DataRowView)((GridViewRow)lnk.NamingContainer).Da taItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl + drv[theColumnCommandArg].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 LinkButtonTemplate class in more pages and grids.

Thank you.

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plschreef in bericht
news:B2**********************************@microsof t.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(object 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("Invoice"))
{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);
Feb 24 '07 #6

P: n/a
Thanks alot Milosz!!!!!!!!!
"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plschreef in bericht
news:2D**********************************@microsof t.com...
You have to recreate the grid because criteria have changed:

protected void SearchButton_Click(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*****@DONTLIKESPAMwp.plschreef in bericht
news:8A**********************************@microso ft.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.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;
using System.Text.RegularExpressions;

public partial class _Default : BasePage
{
protected void Page_Init(object 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("Invoice"))
{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

LinkButtonTemplate template = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

template.LinkButtonClick += new EventHandler(template_LinkButtonClick);

tempColumn.ItemTemplate = template;

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screent.aspx?nav=1&cid=", "FakID");

grvSearchResult.Columns.Add(tempColumn);

}

else
{

BoundField boundColumn = new BoundField();

boundColumn.DataField = col.ColumnName;

boundColumn.HeaderText = col.ColumnName;

grvSearchResult.Columns.Add(boundColumn);

}

}

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

void template_LinkButtonClick(object sender, EventArgs e)
{
//whatever
}
#region LinkButtonTemplate

public class LinkButtonTemplate : ITemplate, INamingContainer
{

string theColumnName = "", thePostBackUrl = "", theColumnCommandArg =
"";

public LinkButtonTemplate(string ColumnName, string PostBackUrl, string
ColumnCommandArg)
{

theColumnName = ColumnName;

thePostBackUrl = PostBackUrl;

theColumnCommandArg = ColumnCommandArg;

}

public event EventHandler LinkButtonClick;

public void InstantiateIn(Control container)
{

LinkButton lnk = new LinkButton();

container.Controls.Add(lnk);

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

if (LinkButtonClick != null)
lnk.Click += new EventHandler(LinkButtonClick);

}

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

void lnk_DataBinding(object sender, EventArgs e)
{

LinkButton lnk = (LinkButton)sender;
DataRowView drv =
(DataRowView)((GridViewRow)lnk.NamingContainer).Da taItem;
lnk.Text = drv[theColumnName].ToString();
//lnk.PostBackUrl = thePostBackUrl +
drv[theColumnCommandArg].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 LinkButtonTemplate class in more pages and grids.

Thank you.

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plschreef in
bericht
news:B2**********************************@microso ft.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(object 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("Invoice"))
{

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&tab=I&cid=", "markID");

grvSearchResult.Columns.Add(tempColumn);

}

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

TemplateField tempColumn = new TemplateField();

tempColumn.HeaderText = col.ColumnName;

tempColumn.ItemTemplate = new LinkButtonTemplate(col.ColumnName,
"~/screen.aspx?nav=1&fid=", "FilID");

grvSearchResult.Columns.Add(tempColumn);

Feb 25 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.