473,748 Members | 8,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding a LinkButton to a gridview, dynamically.

Hi,
Consider the following handler:

protected void gridView_RowDat aBound(object sender,
GridViewRowEven tArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowT ype.DataRow)
return;

Label lblRowNr = (Label)row.Find Control("lblRow Nr");
lblRowNr.Text = String.Format(" {0}.", row.RowIndex + 1);

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

}

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 17923
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...essen tially 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******** *************** ***********@t54 g2000hsg.google groups.com...
Hi,
Consider the following handler:

protected void gridView_RowDat aBound(object sender,
GridViewRowEven tArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowT ype.DataRow)
return;

Label lblRowNr = (Label)row.Find Control("lblRow Nr");
lblRowNr.Text = String.Format(" {0}.", row.RowIndex + 1);

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

}

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******** *************** ***********@t54 g2000hsg.google groups.com...
Hi,
Consider the following handler:

protected void gridView_RowDat aBound(object sender,
GridViewRowEven tArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowT ype.DataRow)
return;

Label lblRowNr = (Label)row.Find Control("lblRow Nr");
lblRowNr.Text = String.Format(" {0}.", row.RowIndex + 1);

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

}

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...@aspalli ance.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...essen tially 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...@mailinato r.comwrote in message

news:60******** *************** ***********@t54 g2000hsg.google groups.com...
Hi,
Consider the following handler:
protected void gridView_RowDat aBound(object sender,
GridViewRowEven tArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowT ype.DataRow)
return;
Label lblRowNr = (Label)row.Find Control("lblRow Nr");
lblRowNr.Text = String.Format(" {0}.", row.RowIndex + 1);
LinkButton lnk = new LinkButton();
lnk.ID = "lnk" + row.RowIndex;
lnk.Text = "Ooops!";
lnk.Click += new EventHandler(ln k_Click); //This handler
(lnk_Click) is never get called!
row.Cells[0].Controls.Add(l nk);
}
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.DataBi nd 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******** *************** ***********@x41 g2000hsb.google groups.com...
On Jul 14, 6:21 pm, "Teemu Keiski" <jot...@aspalli ance.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...essen tially 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...@mailinato r.comwrote in message

news:60******* *************** ************@t5 4g2000hsg.googl egroups.com...
Hi,
Consider the following handler:
protected void gridView_RowDat aBound(object sender,
GridViewRowEven tArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowT ype.DataRow)
return;
Label lblRowNr = (Label)row.Find Control("lblRow Nr");
lblRowNr.Text = String.Format(" {0}.", row.RowIndex + 1);
LinkButton lnk = new LinkButton();
lnk.ID = "lnk" + row.RowIndex;
lnk.Text = "Ooops!";
lnk.Click += new EventHandler(ln k_Click); //This handler
(lnk_Click) is never get called!
row.Cells[0].Controls.Add(l nk);
}
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.DataBi nd 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
4885
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 and can't figure out how to make it work in, for instance, Firefox. Right now the code I am using is as follows: newOpt=document.createElement('OPTION'); newOpt.id='optKeyword'; newOpt.label='Keyword';...
8
2943
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 after the class was modified. I need this to work for both old ('classic') and new style classes, at both 2.3 and 2.4. I of course want to avoid side effects, and to make the solution as light-weight as possible.
1
2486
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
3272
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 always enabled. What could be preventing this control from being disabled. The html looks like this: <asp:LinkButton ID="BusSvcButton" runat="server" CausesValidation="false"
0
1857
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 always revert back to the Design View SelectCommand set in Design mode. I even set it in code and rebind, but when the page is reposted it
3
2059
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) which contains the value "0". How to do this,thanxs in advance for Any kind of suggestion .......
1
3034
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 get the value from the textbox to add to the gridview on the button click. After trying to integrate other solutions into my code I've come up with this garbled mess in the button click event: DataTable paramValues = new DataTable(); ...
0
1280
by: krishnaneeraja | last post by:
Hi I want to add link labels in gridview dynamically in windows application.
2
2196
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 set attributes to the new row added such as id, class etc
0
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8243
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.