473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Adding a LinkButton to a gridview, dynamically.

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
4 17876
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: David | last post by:
Can anyone give me a quick code snippet (that is standards-based) for adding OPTION tags to a SELECT dynamically. I have no problem doing it in IE but I am kind of new to the whole standards world...
8
by: Kevin Little | last post by:
#!/usr/bin/env python ''' I want to dynamically add or replace bound methods in a class. I want the modifications to be immediately effective across all instances, whether created before or...
1
by: Sheryl Landon | last post by:
Is there a way to set the OnCommand or OnClick attributes when adding a LinkButton dynamically through code? These attributes are available when adding on design-time... Thanks, Sheryl
0
by: Keithb | last post by:
My application has a LinkButton that I am using as a Command button in the ItemTemplate of a GridView control. Regardless of the value set for the Enabled property (true or false), The control is...
0
by: rogoflap | last post by:
I have a SQLDatasource I created on a form. It is tied to the GridView on that form. I would like to dynamically change the SQLStatement based on some criteria on the form. It seems to...
3
by: giveDsolution | last post by:
Hello Frds, I have bind a gridview with the help of a Datatable as the columns are added dynamically. But my problem is i have to add a gridview in any cell (which will be bind by another Query)...
1
by: bJames | last post by:
Hi I've been all around looking for an answer to this question, but can't find it. I've got a gridview with a single unbound column. It's got a textbox and button in the footer row. I"m trying to...
0
by: krishnaneeraja | last post by:
Hi I want to add link labels in gridview dynamically in windows application.
2
by: romepatel | last post by:
Hello, I am adding a row dynamically to the table , var table = document.getElementById('example'); var rowCount = table.rows.length; var row = table.insertRow(rowCount); now i want to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.