views:

32

answers:

1

I have a custom user control that consists of a dynamic list of controls, along with a static button that is declared (along with the OnClick declaration) on the aspx page. The button shows or hides a panel containing the dynamic control list.

I am finding a problem that I presume is related to the dynamically added controls, where the button event method (even though it is not added dynamically), only fires on the second click. I think this has something to do with the fact that the button event method is wired up before the controls are created, and the event method gets wired up incorrectly on postback when there are suddenly new dynamic controls on the page. On the second click it works because the dynamic controls have all been created and are simply being persisted.(Please someone correct me if I am wrong - I spent all day tracking this down :( )

My question is, how do I fix this? I tried wiring up the event for the button dynamically as well, but I am not sure what order to put all these declarations in. Any ideas?

A: 

Are you sure that button posts back on on first click? Otherwise, it might be something related at client side. Assuming that button does posts back on the first time and its a submit button (UseSubmitBehavior="true") then issue can be due to

  1. User control may be getting added after post data processing has happened (post data processing is responsible for raising button event)
  2. For some reason, button control id (Unique Id) changes across post backs or button id changes from time of post data processing to the time of rendering the control.

In case, you are not able to solve the issue, you can go alternate way - by looking into the post data such as

if (Request.Form[button.UniqueID] != null) { // Do button click processing }
VinayC