tags:

views:

120

answers:

3

There is an edit button on the page upon landing in an updatepanel. User clicks edit button, the event that fires removes this button from the container and adds a save button (in the updatepanel). This function also attaches an event to the new button.

New button shows up on the page but clicking on it does not fire the event. Obviously event does not register. There is probably a trick to make this happen in the updatepanel but I could not figure it out. Does anybody know the answer?

Thanks in advance...

+2  A: 

When you add controls dynamically, you must regenerate them on every postback, other way the events won't be fired.

You must regenerate the controls on the page_init event.

I will do it like this:

i) write a "generateControl" function to generate the control.

ii) call this function when you want to generate it and set a flag (a public variable to true)

iii) on page_init check if the flag is true and call the "generateControl" function if it is.

tekBlues
Okay but how do I do that when I add it for the first time? Cause page_init will fire before the event that creates the button fires. Thanks!
eakkas
I thought the page_init only fires the first time page is created and is ignored on postbacks but I guess that is not true.I will try your suggestion but still confused with one thing. If I attach ii to a button, won't that event fire after page_init during postback and thus flag will simply be ignored?
eakkas
There's nothing wrong with adding the control later than page_init (i.e. during a _click handler) - but you have to be able to generate it earlier for the button to receive postbacks.
DDaviesBrackett
+2  A: 

Is there a reason you can't have both the add and save buttons on the page, with the save button's Visible property set to false except when an add is going on? That way, you get to take advantage of ViewState (i.e. your button will keep visibility between page loads) for very mild cost.

DDaviesBrackett
That's the solution that I currently have implemented but somehow it did not sound right so I was looking for a more elegant solution... Thanks for the input...
eakkas
I myself like this solution better than creating dynamically the control. Simplicity rules!
tekBlues
A: 

I have the same problem, except my problem is adding a GridView. If I add it and set the visible to false, the page loads slow because there is a lot to load in the grid. So I need to find a solution where the user clicks on the "show grid" button and then dynamically add it. So far my problem is the same, the grid adds just fine but it won't register the sort events, so after adding it, I cannot sort the grid.