AJAX ScriptManager in UserControl
Asked Answered
P

5

4

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?

Peridotite answered 16/3, 2009 at 15:34 Comment(0)
S
2

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?

Sixteenmo answered 16/3, 2009 at 15:38 Comment(2)
No there are no master pages....Don't get me started :) My hands are tied on that decision.Peridotite
why is this the answer?Fracas
F
11

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

Foxglove answered 16/3, 2009 at 16:21 Comment(2)
Sorry I don't have an cannot have a masterpage. It's an existing system and we're not allowed to go through and change all of the existing pages...That's why i'm trying to put the smarts in the usercontrol.Peridotite
what would ~/Scripts/myscript.js contain?Canard
S
2

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?

Sixteenmo answered 16/3, 2009 at 15:38 Comment(2)
No there are no master pages....Don't get me started :) My hands are tied on that decision.Peridotite
why is this the answer?Fracas
Q
0

I think the Init event should work. Use the technique you described (i.e. checking that ScriptManager.GetCurrent returns null before adding) but when you add the ScriptManager, make sure you add it as the first control inside the form.

I haven't tested this but I think it will work.

Quentin answered 16/3, 2009 at 16:32 Comment(1)
Sorry, as i said, the Init event isn't even hit because an exception is thrown earlier...Peridotite
A
0

What worked for me was including this line after < form >

 <asp:ScriptManager ID="scriptManager" runat="server"/>

That got me around the "...requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it."

Alarum answered 15/5, 2012 at 15:14 Comment(0)
T
0

I have made 2 things that works.

On the UserControl make this Sub:

Public Sub SetFocus()
    txtBox.Focus()
End Sub

On the caling site, call this before your ScriptManagerCall

userControl.SetFocus()

If you have AutoPostback on you UserControl you also have to set the focus in TextChanged event.

Protected Sub txtBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBox.TextChanged
    txtBox.Focus()
    ......
End Sub

Hope this works.

Torrent answered 26/3, 2015 at 12:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.