| | |
Button click event only working the second time
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Nov 2005
Posts: 24
Reputation:
Solved Threads: 0
I dynamically create some linkbuttons and assign them the same EventHandler:
In the engCat_Click event, it sets a static variable to the value of the LinkButton's ID that was clicked:
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!
C# Syntax (Toggle Plain Text)
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:
C# Syntax (Toggle Plain Text)
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!
•
•
Join Date: Nov 2005
Posts: 24
Reputation:
Solved Threads: 0
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...
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...
•
•
Join Date: Dec 2004
Posts: 1,655
Reputation:
Solved Threads: 35
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
This article deals very specifically with that issue:
ASP.NET Page Life Cycle and Dynamic Controls
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.
![]() |
Similar Threads
- Passing value of string from one button click event to another (ASP.NET)
- Calling button-click events from different classes (Python)
- Click event >> window appears? (Python)
- Control button Click event (Visual Basic 4 / 5 / 6)
Other Threads in the C# Forum
- Previous Thread: ascx control values in aspx page
- Next Thread: Dynamically load and use wpf usercontrols
| Thread Tools | Search this Thread |
.net access ado.net algorithm array bitmap box buttons c# check checkbox client combobox connection console contorl control conversion cryptographyc#winformsencryption csharp database datagrid datagridview dataset datetime degrees deployment developer disabled displayingopenforms draganddrop drawing editor encryption enum excel file form format forms function gdi+ hospitalmanagementinformationsystem i18n image imageprocessing index-error input install java label list listbox mandelbrot math mathematics mouseclick mysql operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remoting richtextbox rows serialization server setup sleep socket sql statistics stream string table temperature text textbox thread time timer totaldays update uploadatextfile user usercontrol validation visualstudio webbrowser windowsformsapplication winforms wpf xml






