Adding a LinkButton to a gridview, dynamically.

  • Thread starter Thread starter jack
  • Start date Start date
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
 
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
 
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
 
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.
 
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.
 
Back
Top