0

I have a unique problem. I'm working on a .net solution where I do not have access to the main aspx pages. Mainly, I cannot modify any of them. I'm building a whole bunch of usercontrols, that will get used on various .aspx page. I want to use the updatepanel, and one knows it requires the scriptmanager control to be on the page.

So - what I do have access to is a placeholder on the .aspx page. What I want to try and do is place the scriptmanager control in this placeholder. I already know how to do this, by getting a reference to the context page and then adding a control in there. However, I'm having an issue with the timing. I have the updatepanel on the .ascx control, and I have the scriptmanager load in the Page_PreLoad event of the control. This doesn't seem to work, as it complains that "'The control with ID 'updUsers' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.' Its obvious the updatepanel is loading before the Page_Preload event.

Question is, exactly what method can I use to load the control? Can this be done at all? Is there a better way to do this?

EDIT:

I've tried almost all the events:

protected void Page_PreInit(object sender, EventArgs e)
protected override void OnInit(EventArgs e)

DETAILS

Just to clarify, this is what the usercontrol code looks like:

public partial class PollModule : ModuleBase
    {
        protected override void OnInit(EventArgs e)
        {
          Page page = HttpContext.Current.Handler as Page;
          page.Init += delegate(object sender, EventArgs e_Init)
          {
            if (ScriptManager.GetCurrent(page) == null)
            {
                ScriptManager sMgr = new ScriptManager();
                page.Form.Controls.AddAt(0, sMgr);
            }
          };
          base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }

SOMEWHAT WORKING EDIT

I was able to get it partially working by attaching to the updatepanel init, so I know however way I am adding the scriptmanager is working, so I guess I am just missing the right page event to attach to.

M.R.
  • 4,737
  • 3
  • 37
  • 81

1 Answers1

0

If it's a simple matter of using ScriptManager in a UserControl - you can use ScriptManagerProxy instead.

This way all your AJAX controls will compile and run and there won't be a conflict with main page's ScriptManager

Just declare it in control markup, e.g.

<%@ Control Language="CSharp" AutoEventWireup="false" CodeFile="MyControl.cs" Inherits="MyControl" %>

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
    <Services>

    </Services>
    <Scripts>

    </Scripts>
</asp:ScriptManagerProxy>
Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
  • Well, I could have other usercontrols that also use the scriptmanager, and they could be on the same page. I don't want them to clash. – M.R. Aug 23 '13 at 20:37
  • They won't. You can have multiple user controls on a page, each with its own ScriptManagerProxy - they will not conflict with each other, neither with one and only already existing main ScriptPanager of the page itself. That's the purpose of the "proxy" – Yuriy Galanter Aug 23 '13 at 20:41
  • Doesn't it actually at least one scriptmanager on the page already? Thats my main problem anyway. I wouldn't need it if I had a scriptmanager on the page already... – M.R. Aug 23 '13 at 20:42
  • Yup the main page (the one you don't have access to) must have scriptmanager already - but your user control cannot have it then otherwise they will conflict. – Yuriy Galanter Aug 23 '13 at 20:50
  • I think you missed my intent. I don't have access to main page, and it DOESN'T have the scriptmanager, and I'm trying to add it dynamically from my usercontrol. If the main page already had it, I wouldn't need anything in the usercontrol (I think) – M.R. Aug 23 '13 at 20:52
  • Yes, I get it that you don't have access to the page, but you did not mention that page has no ScriptManager of its own. In that case no, this solution wont work for u – Yuriy Galanter Aug 23 '13 at 20:57
  • I don't think I would have this question to ask if the scriptmanager was there :) – M.R. Aug 23 '13 at 20:59
  • My bad :) I mistook the scenario. But do search SO, it looks like an existing problem that was solved before (I know you tried Init already) but maybe something is different e.g. http://stackoverflow.com/a/184262/961695 – Yuriy Galanter Aug 23 '13 at 21:14