473,503 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding EventHandler to LinkButton that I create programatically

Hi,
I am trying to dynamically create linkbuttons. They need an event handler,
so i can respond to the user's click.
I try to add the eventhandler on the fly, but when i click on the link, the
code does not execute, it just reloads the page.

Where am i going so wrong? i don't understand what's missing.

many thanks in advance.

//Creating the link buttons
for(int i=0; i<4; i++)
{
LinkButton lbtn = new LinkButton();
lbtn.ID = i;
lbtn.Text = "Delete";
btn.Click += new EventHandler(lbtn_Click);
}

//Event Handler
private void lbtn_Click(object sender, System.EventArgs e)
{
LinkButton lbtnSender = (LinkButton)sender;
Response.Write(lbtnSender.ID.ToString());
}
Nov 17 '05 #1
3 23178
Hi CodeRazor,

Make sure you create exactly the same set of controls with the same IDs and
property values upon PostBack. The whole hierarchy of controls must be
exactly the same as it was when the page was rendered for the first time.

It also matters *WHEN* you create the dynamic controls. At least they must
be created before the PreRender phase.
Also, make sure to add the created controls to the Page's Controls
collection (not sure about this step, did not touch ASP .NET for more than a
year).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com...
Hi,
I am trying to dynamically create linkbuttons. They need an event handler,
so i can respond to the user's click.
I try to add the eventhandler on the fly, but when i click on the link,
the
code does not execute, it just reloads the page.

Where am i going so wrong? i don't understand what's missing.

many thanks in advance.

//Creating the link buttons
for(int i=0; i<4; i++)
{
LinkButton lbtn = new LinkButton();
lbtn.ID = i;
lbtn.Text = "Delete";
btn.Click += new EventHandler(lbtn_Click);
}

//Event Handler
private void lbtn_Click(object sender, System.EventArgs e)
{
LinkButton lbtnSender = (LinkButton)sender;
Response.Write(lbtnSender.ID.ToString());
}


Nov 17 '05 #2
"Make sure you create exactly the same set of controls with the same IDs and
property values upon PostBack"

that's the solution.
thank you dmytro.

CR.
Nov 17 '05 #3
JJ
Hi,

If I had another set of controls how can I go about doing it.

My page is a view photo page. Lets say there is 3 pages of 20 photo each and
a large placeholder to display a selected photo from the sets of photo. When
I move from page 1 to page 2, i had a new sets of photo display hence from
the post you were mentioning that this will not work. True enf the click
event does not fire off after the page change from 1->2.

Page 2 is reload after a selection, i.e. page 2 controls are exactly the
same as the previous page 2 components hence the event now is able to fire
off.

So how can I overcome this? Force a page reload when page renders from 1->2
or 2->1? How can i do this?Or Whats the code to write for asp.net to treat it
as a new posting?

Thanks!
JJ

"Dmytro Lapshyn [MVP]" wrote:
Hi CodeRazor,

Make sure you create exactly the same set of controls with the same IDs and
property values upon PostBack. The whole hierarchy of controls must be
exactly the same as it was when the page was rendered for the first time.

It also matters *WHEN* you create the dynamic controls. At least they must
be created before the PreRender phase.
Also, make sure to add the created controls to the Page's Controls
collection (not sure about this step, did not touch ASP .NET for more than a
year).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com...
Hi,
I am trying to dynamically create linkbuttons. They need an event handler,
so i can respond to the user's click.
I try to add the eventhandler on the fly, but when i click on the link,
the
code does not execute, it just reloads the page.

Where am i going so wrong? i don't understand what's missing.

many thanks in advance.

//Creating the link buttons
for(int i=0; i<4; i++)
{
LinkButton lbtn = new LinkButton();
lbtn.ID = i;
lbtn.Text = "Delete";
btn.Click += new EventHandler(lbtn_Click);
}

//Event Handler
private void lbtn_Click(object sender, System.EventArgs e)
{
LinkButton lbtnSender = (LinkButton)sender;
Response.Write(lbtnSender.ID.ToString());
}


Jan 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1433
by: Simon Green | last post by:
Hi You peops are probably going to get sick of me over the next few weeks :) I am trying to add a button via code when an existing button is clicked. I have the following bits of code...
4
4300
by: Neven Klofutar | last post by:
Hi, How can I add an even handler to a control that is not situated on the main page (it's situated in the repeater). How do I rewrite this to make it work ? Thanx, Neven ...
0
955
by: Lars Netzel | last post by:
Hey! I'm adding a ButtonColumn (Push button) in a datagrid in Runtime. How do I write the code to add the eventhandler for this buttons Click Event? Best regards /Lars
0
1540
by: BLiTZWiNG | last post by:
I have scoured the web to no avail. Any post that resembles my problem has no answer. I have an asp table on my page. I add rows and cells to it at run time with info provided from a database in...
0
2046
by: mazdotnet | last post by:
Hi, I'm using asp:TreeView to display information on our website. I like to add a LinkButton that fires an event when clicked to the TreeNode. TreeNode childNode = new TreeNode("Download...
2
1810
by: dtarczynski | last post by:
Hello. Im trying to add new EventHandler to DropDownList programically. Im doing something like this: DropDownList ddlCate = new DropDownList(); ddlCate.ID = String.Format("ddlCategory{0}",...
2
1404
by: Jim Andersen | last post by:
I have this piece of code I am trying to convert from c# to vb.net. It's a Windows project. This line gives me trouble ( I have tried to compress the code for readability. Hope I haven't screwed...
5
1815
by: hmkaddoura | last post by:
Hi all, I have a question... I'm creating tabs programmatically using vb.net . in each tab that is created there is listbox, and 4buttons (also added by coding). when i change the listbox...
4
17884
by: jack | last post by:
Hi, Consider the following handler: protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow row = e.Row; if (row.RowType != DataControlRowType.DataRow)...
0
7205
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
7287
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,...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
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
5594
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,...
1
5022
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...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.