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

Adding a LinkButton to a gridview, dynamically.

P: n/a
Hi,
Consider the following handler:

protected void gridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowType.DataRow)
return;

Label lblRowNr = (Label)row.FindControl("lblRowNr");
lblRowNr.Text = String.Format("{0}.", row.RowIndex + 1);

LinkButton lnk = new LinkButton();
lnk.ID = "lnk" + row.RowIndex;
lnk.Text = "Ooops!";
lnk.Click += new EventHandler(lnk_Click); //This handler
(lnk_Click) is never get called!
row.Cells[0].Controls.Add(lnk);

}

I really don't get why the lnk_Click function is never get called.

Would you please let me know how am I supposed to do this?

Thanks
Jack
Jul 14 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

the controls should be added in RowCreated because RowDataBound fires only
when DataBind() is called, and it could be a postback when no databinding
happens, when button is clicked...essentially on postback, it might not
exist at that point... and for a control to raise the event, it must exist
in Controls collection at the proper time

Basic idea is described in following post on ASP.NET Forums:
http://forums.asp.net/p/745417/745492.aspx

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
"jack" <at*@mailinator.comwrote in message
news:60**********************************@t54g2000 hsg.googlegroups.com...
Hi,
Consider the following handler:

protected void gridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowType.DataRow)
return;

Label lblRowNr = (Label)row.FindControl("lblRowNr");
lblRowNr.Text = String.Format("{0}.", row.RowIndex + 1);

LinkButton lnk = new LinkButton();
lnk.ID = "lnk" + row.RowIndex;
lnk.Text = "Ooops!";
lnk.Click += new EventHandler(lnk_Click); //This handler
(lnk_Click) is never get called!
row.Cells[0].Controls.Add(lnk);

}

I really don't get why the lnk_Click function is never get called.

Would you please let me know how am I supposed to do this?

Thanks
Jack

Jul 14 '08 #2

P: n/a
You are creating the link dynamically. If you want to use dynamically
created object in postbacks, you need to re-create them on every postback in
Page_PreInint or _Init event.

I would recommend including the link into ItemTemplate and hiding it as
necessary. Then you can setup the event handler in the markup. No need to
create the link dynamically.
--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"jack" <at*@mailinator.comwrote in message
news:60**********************************@t54g2000 hsg.googlegroups.com...
Hi,
Consider the following handler:

protected void gridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowType.DataRow)
return;

Label lblRowNr = (Label)row.FindControl("lblRowNr");
lblRowNr.Text = String.Format("{0}.", row.RowIndex + 1);

LinkButton lnk = new LinkButton();
lnk.ID = "lnk" + row.RowIndex;
lnk.Text = "Ooops!";
lnk.Click += new EventHandler(lnk_Click); //This handler
(lnk_Click) is never get called!
row.Cells[0].Controls.Add(lnk);

}

I really don't get why the lnk_Click function is never get called.

Would you please let me know how am I supposed to do this?

Thanks
Jack

Jul 14 '08 #3

P: n/a
On Jul 14, 6:21 pm, "Teemu Keiski" <jot...@aspalliance.comwrote:
Hi,

the controls should be added in RowCreated because RowDataBound fires only
when DataBind() is called, and it could be a postback when no databinding
happens, when button is clicked...essentially on postback, it might not
exist at that point... and for a control to raise the event, it must exist
in Controls collection at the proper time

Basic idea is described in following post on ASP.NET Forums:http://forums.asp.net/p/745417/745492.aspx

--
Teemu Keiski
AspInsider, ASP.NET MVPhttp://blogs.aspadvice.com/jotekehttp://teemukeiski.net

"jack" <a...@mailinator.comwrote in message

news:60**********************************@t54g2000 hsg.googlegroups.com...
Hi,
Consider the following handler:
protected void gridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowType.DataRow)
return;
Label lblRowNr = (Label)row.FindControl("lblRowNr");
lblRowNr.Text = String.Format("{0}.", row.RowIndex + 1);
LinkButton lnk = new LinkButton();
lnk.ID = "lnk" + row.RowIndex;
lnk.Text = "Ooops!";
lnk.Click += new EventHandler(lnk_Click); //This handler
(lnk_Click) is never get called!
row.Cells[0].Controls.Add(lnk);
}
I really don't get why the lnk_Click function is never get called.
Would you please let me know how am I supposed to do this?
Thanks
Jack
Well, thank you. I now understand how the gridview control works.
Based on those articles I've already read, I've got two options:

1. In the occurrence of RowDataBound event, I need to save what I need
to create dynamically, in a ViewState. Then, in the RowCreated, I need
to recreate the controls based on the saved ViewStates.

2. On the Load event, I'll call the gridView.DataBind method, on
PostBack conditions.

The ViewState thing is really hard to do (in my scenario), since the
controls I'm creating are a lot.

Is there any better way to do this? Please note that I need the
DataItem to create my controls dynamically.
Jul 14 '08 #4

P: n/a
Hi,

using a template e.g implementing ITemplate or custom field/column might
offer a bit cleaner route.

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
"jack" <at*@mailinator.comwrote in message
news:5d**********************************@x41g2000 hsb.googlegroups.com...
On Jul 14, 6:21 pm, "Teemu Keiski" <jot...@aspalliance.comwrote:
>Hi,

the controls should be added in RowCreated because RowDataBound fires
only
when DataBind() is called, and it could be a postback when no databinding
happens, when button is clicked...essentially on postback, it might not
exist at that point... and for a control to raise the event, it must
exist
in Controls collection at the proper time

Basic idea is described in following post on ASP.NET
Forums:http://forums.asp.net/p/745417/745492.aspx

--
Teemu Keiski
AspInsider, ASP.NET
MVPhttp://blogs.aspadvice.com/jotekehttp://teemukeiski.net

"jack" <a...@mailinator.comwrote in message

news:60**********************************@t54g200 0hsg.googlegroups.com...
Hi,
Consider the following handler:
protected void gridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowType.DataRow)
return;
Label lblRowNr = (Label)row.FindControl("lblRowNr");
lblRowNr.Text = String.Format("{0}.", row.RowIndex + 1);
LinkButton lnk = new LinkButton();
lnk.ID = "lnk" + row.RowIndex;
lnk.Text = "Ooops!";
lnk.Click += new EventHandler(lnk_Click); //This handler
(lnk_Click) is never get called!
row.Cells[0].Controls.Add(lnk);
}
I really don't get why the lnk_Click function is never get called.
Would you please let me know how am I supposed to do this?
Thanks
Jack

Well, thank you. I now understand how the gridview control works.
Based on those articles I've already read, I've got two options:

1. In the occurrence of RowDataBound event, I need to save what I need
to create dynamically, in a ViewState. Then, in the RowCreated, I need
to recreate the controls based on the saved ViewStates.

2. On the Load event, I'll call the gridView.DataBind method, on
PostBack conditions.

The ViewState thing is really hard to do (in my scenario), since the
controls I'm creating are a lot.

Is there any better way to do this? Please note that I need the
DataItem to create my controls dynamically.

Jul 14 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.