This is a migrated thread and some comments may be shown as answers.

Event Won't Be Subscribed To On Load on Demand Tab

6 Answers 63 Views
TabStrip
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 12 Oct 2011, 02:56 PM
Hi,

A colleague and I have run into problems hooking up an event for a button click on a user-control which is being dynamically loaded.
Basically, we are trying to get tabs to load on demand.
We have done up an example which can be downloaded here (we've used the dll - Telerik.Web.UI, Version=2011.2.712.40 and its skin dll Telerik.Web.UI.Skins.dll).

The button on the user control can be seen if you just load the Default page. If you click the button, a post ensues (as per the normal input/forms web model) but the click handler never catches the fired event. Debugging that handler demonstrates this. In fact, we're not 100% sure that the event fired. All we know is that the form posts to the server.

What do we need to do to successfully subscribe to that click event? This is clearly as a result of dynamically loading the usercontrol. We just cannot figure out "the why", and how to fix it. It seems as though the javascript which is normally rendered by the Button server control is not being hooked up properly. Perhaps it's a ViewState thing?

Cheers

6 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar Terziev
Telerik team
answered on 17 Oct 2011, 09:06 AM
Hi David,

The reason for the experienced behavior is the following.  The event is not fired since upon post-back the user control which you have added dynamically does not exists in the controls collection of the page. This is due to the fact that when you add controls dynamically you should add them upon every post-back, otherwise they are gone.

When loading controls dynamically it's convenient to use the PageViewCreated event, since this event is fired upon every post-back.

Best wishes,
Dimitar Terziev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
David
Top achievements
Rank 1
answered on 18 Oct 2011, 03:08 PM
What is the reason for that?
Why is it not possible to create a control, only on the PostBacks where that tab was clicked, and hooking up the event after it has been created?
I have watched the debugger move through the line of code that hooks up the handler. The control does exist. It just doesn't fire.
Is there some kind of ASP.NET "smoke and mirrors" working in the background which I am not understanding?

Cheers
0
David
Top achievements
Rank 1
answered on 20 Oct 2011, 01:58 PM
I think I understand this now.
The control was not in the "tree of controls" when the page posts back (as a result of the button click). This is because I was adding it in the Tab Click event (a postback event handler of the tab control).
That is too late for the button to be added to the control tree. Am I on the right track?
0
Dimitar Terziev
Telerik team
answered on 21 Oct 2011, 12:07 PM
Hello David,

You could add controls in the tab click event, but unless you add these controls again when the page makes post-back they will be gone since they are not part of the controls collection of the page. The suggestion to use PageViewCreated solves this problem since this event is fired upon every post-back when the RadMultiPage is recreated.

All the best,
Dimitar Terziev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
David
Top achievements
Rank 1
answered on 22 Oct 2011, 01:23 PM
OK. I have looked at this further and now believe that my last statement is incorrect.
Dimitar is also incorrect.
Dimitar's solution worked. But his reason for it is not correct.
The dynamic control IS in the control tree when the page is loaded. I ran a trace and can confirm that the control is there.

I believe the problem is a combination of the fact that the control is not loaded on every PostBack and ViewState.
The state of ViewState for each page load is out of sync with the control tree. This is because the controls are being loaded after ViewState is serialized each time.
I am going to keep researching this and try and prove it with some code. Stay tuned. THIS is an interesting one.
0
David
Top achievements
Rank 1
answered on 21 Nov 2011, 09:22 AM
I just wanted to publish the reason for the dynamic controls problem that I was having, which I have now come to understand.

Basically, the problem is that in each request/response sequence, the ViewState was getting out of sync with the control tree. To see the problem again, you can download this small project, run it and click the Update button on the front tab. You will notice two things. The text in the control above it disappears and the event handler which I hooked up to that update button does not get hit.

I have been able to prove my theory with code, which you can download in this project. That project manually reconstructs the relationship between ViewState and the control tree. Note: I would never implement the functionality using code like that, as it was just an academic exercise to understand exactly what was going on with those dynamic controls. It also explains why Dimitar's proposed solution works. You will see in that version of the code that the Update button does hit its handler, when clicked.

I hope this helps others in their understanding of the complex relationship between ViewState, dynamic controls and the control tree.

Cheers
Tags
TabStrip
Asked by
David
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
David
Top achievements
Rank 1
Share this question
or