By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,636 Members | 1,693 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,636 IT Pros & Developers. It's quick & easy.

Adding EventHandler to LinkButton that I create programatically

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
"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

P: n/a
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.