954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Button click event only working the second time

I dynamically create some linkbuttons and assign them the same EventHandler:

lb.Click  += new System.EventHandler(engCat_Clicked);


In the engCat_Click event, it sets a static variable to the value of the LinkButton's ID that was clicked:

protected void engCat_Clicked(Object sender, EventArgs e) 
{
	LinkButton lb = (LinkButton)sender;
	string engcatID = lb.ID;
	currentEngCatID = engcatID;
}

The problem is that this variable is only set the second time I click on the button. It seems the EventHandler method is only called the second time the event happens.

Has anyone encountered this kind of problem before, or know what might be the cause?
Thanks!

vangraan
Newbie Poster
24 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

where is this ("lb.Click += new System.EventHandler(engCat_Clicked);") piece of code located?

without seeing all of the code, i would hazard a guess that it is inside an
if(Postback){
}
section

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

No it's not in if (Postback), it is just plain in the Page_Load. I have tried adding it in the PageInit as well (another forum suggested this) but no difference. I think it definately has something to do with creating the buttons dynamically, but it's the only way to solve my problem.

I will try pasting some more of my code to make it clearer..

Maybe I should just fire the button event automatically when the page is busy loading? I have run out of ideas...

vangraan
Newbie Poster
24 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

Sounds like an ASP.NET application? If so, and if you're working with dynamically-created controls, then you're very likely running afoul of the ASP.NET Page LifeCycle.

This article deals very specifically with that issue:

ASP.NET Page Life Cycle and Dynamic Controls

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

I know this is an old post, but I recently ran into a similar problem involving a linkbutton contained within a dynamically loaded user control inconsistently requiring two clicks in order to reach its Click handler method. Most of the posts I found kept referring people to the page lifecycle, but in the end that had nothing to do with it. The issue was that the controls ID values were being assigned automatically by .NET so they were {ctl001, ctl002, ctl003...} etc. Because the page allowed users to add or remove sections by clicking on linkbuttons, these ID values were capable of changing between page loads. This would cause the dynamically loaded controls to receive different IDs than they had on the previous page load and would result in a screwed up viewstate. .NET could not identify which control went with which viewstate/handler. After I adopted a less dynamic naming method, based on a root string prefix and a numeric suffix based on a database primary key, the issue was resolved. Because my controls were loaded in a pretty deep nest, there was more than one place where a dynamically generated ID could be introducing this problem. So I would reccomend to anybody dealing with this issue, view source in your browser and look at the IDs assigned to your controls. If there are a lot of ctl002, ctl003, ctl004 type of control IDs in your html try to eliminate those by assigning more explicit and static names that will not be affected by the addition or removal of other controls on the page.

dogTate
Newbie Poster
1 post since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

Thank you dogTate, I had the same problem.
It actually wasn't the first time I got screwed by forgetting to give IDs to controls.

kdelta
Newbie Poster
1 post since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Yeah, thx.
I'm with you, kdelta.
It's remarkable how you can forget such a simple thing over and over again...*jeez* ;-)

madmital
Junior Poster
120 posts since Jun 2005
Reputation Points: 10
Solved Threads: 5
 

Thanks dogTate. Saved me a lot of time...

arnhemb
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

This may caused that your IIS cross referencing of events.

Simply reset IIS.

Goto Run type iisreset and press Ok.

rawat_raju1985
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You