Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX ScriptManager in UserControl

I have a UserControl that contains an UpdatePanel which wraps some other controls. The UserControl will be used on some pages that already have a ScriptManager and other pages that do not have a ScriptManager. I'd like the UserControl to automatically bring its own ScriptManager if one does not exist.

I have tried ScriptManager.GetCurrent and if it returns null i create my own ScriptManager and insert it into the Form but I can't find a place early enough in the UserControl's lifecycle to run this code. I keep getting the error "The control with ID 'uPnlContentList' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it." every time i try loading the page. The places i've tried running my code are OnInit, CreateChildControls and PageLoad and they never get called because it dies before reaching them.

Where should I put this check?

like image 868
cjserio Avatar asked Dec 14 '22 05:12

cjserio


2 Answers

put a ScriptManager in your MasterPage that all your ASPX files reference. Then anywhere else in the site that you need to work with the ScriptManager use a ScriptManagerProxy that will get the ScriptManager from the master page and let you work with it in the user control and in the code behind.

Now calling ScriptManager.GetCurrent(Page) will give you a reference to a script manager.

<asp:ScriptManagerProxy>
  <Scripts>
    <asp:ScriptReference Path="~/Scripts/myscript.js" />
  <Scripts>
</asp:ScriptManagerProxy>

Here's a link:

MSDN Info

like image 58
Dean Poulin Avatar answered Dec 29 '22 11:12

Dean Poulin


I hate to come at this in another direction, but are you using a master page? And if so, have you considered placing a single ScriptManager on it and being done with it?

like image 33
Daniel Henry Avatar answered Dec 29 '22 12:12

Daniel Henry