Adding EventHandler to LinkButton that I create programatically

G

Guest

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());
}
 
D

Dmytro Lapshyn [MVP]

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).
 
G

Guest

"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.
 
G

Guest

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 said:
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 said:
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());
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top