Adding a LinkButton to a gridview, dynamically.

J

jack

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
 
T

Teemu Keiski

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
 
E

Eliyahu Goldin

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
 
J

jack

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


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.
 
T

Teemu Keiski

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 said:
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


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top