471,599 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 23053
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Simon Green | last post: by
4 posts views Thread by Neven Klofutar | last post: by
reply views Thread by BLiTZWiNG | last post: by
reply views Thread by mazdotnet | last post: by
2 posts views Thread by dtarczynski | last post: by
2 posts views Thread by Jim Andersen | last post: by
2 posts views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.