views:

403

answers:

1

How can I access the main page object in the AddedControl event of a user control?

I am trying to run the line of code ScriptManager oSCM = ScriptManager.GetCurrent(this.Page); in that event, but it returns null, even if there is a scriptmanager on the parent page. In the OnInit event in the WUC that same line of code returns the scriptmanger object, but not in the addcontrol. Am I using the page object wrongly in that event - do I need to access it differently?

Cheers

MH

+1  A: 

You need to take a look at the ASP.NET Page Life Cycle, see this post on SO and my answer to it:

Where should stuff be done in an ASP.NET page

Colin
Thanks for that, but I think I need a bit more detailed view of the early part. I have done a lot of debugging (having overridden every event I can see in the user control, so I can see what's firing when) and I can the flow going through the AddParsedSubObject and AddedControl stages - it seems to get through them ok for the update panel as it loads a control I inserted after the panel, but then it hits the unload events as the error is raised, I need to know where the "No script manager present" error is occuring, and whether the page is instantiated by that point.....
Mad Halfling
....so I can then use the ScriptManager.GetCurrent(Page) method to see if there is the SCM instantiated, and add it if not. The problem is that at the AddedControl stage (as I think I mentioned) the page is still null so I need to do it after then. I suspect that I may be in a chicken-and-egg scenario as the page maybe isn't being made available until after the "No scriptmanager" error is being raised, so I can't then test for the existence of the scriptmanager and insert one before the error kicks in. Or is there a way of using the SCM GetCurrent method (or something similar)....
Mad Halfling
....to check, from the user control, for the existence of a scriptmanager at this earlier stage?CheersMH
Mad Halfling
That was useful information, but I have since found that this can't be done.
Mad Halfling