views:

12811

answers:

7

I am developing a WebPart (it will be used in a SharePoint environment, although it does not use the Object Model) that I want to expose AJAX functionality in. Because of the nature of the environment, Adding the Script Manager directly to the page is not an option, and so must be added programmatically. I have attempted to add the ScriptManager control to the page in my webpart code.

protected override void CreateChildControls()
{
    if (ScriptManager.GetCurrent(Page) == null)
    {
        ScriptManager sMgr = new ScriptManager();
        // Ensure the ScriptManager is the first control.
        Page.Form.Controls.AddAt(0, sMgr); 
    }
}

However, when this code is executed, I get the following error message:

"The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases."

Is there another way to add the ScriptManager to the page from a WebPart, or am I going to have to just add the ScriptManager to each page (or master page) that will use the WebPart?

+1  A: 

There is an alternative to ScriptManager for using the AJAX Control Toolkit in cases like ASP.NET MVC, where dropping controls onto a WebForm isn't always an option. You can get the script file only version of the library, and use that. There is an example here.

Craig Stuntz
The example is now here I think. http://stephenwalther.com/blog/archive/2008/03/14/using-asp-net-ajax-with-asp-net-mvc.aspx
Si Keep
Different page, but I fixed the link. Thanks for the heads-up!
Craig Stuntz
+15  A: 

I was able to get this to work by using the Page's Init event:

protected override void OnInit(EventArgs e)
{
    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);
}
Kyle Trauberman
I wonder if this can lead to having more than one ScriptManager on the page...
vitule
No, If you notice, I am checking to ensure that there is no ScriptManager on the page before adding one.
Kyle Trauberman
Is this OnInit a method of the control?
Bryan
Yes. I'm using it in a WebPart, but thats just a control.
Kyle Trauberman
Doesn't work with update panels... Correct way to set the script manager via `Page.Items[typeof(ScriptManager)] = newScriptManager`.
ControlFlow
@ControlFlow: My implementation works fine for me with an UpdatePanel.
Kyle Trauberman
A: 

I've also used the code posted here with jQuery in web parts. It works quite nicely, as long as you set up your .js files as embeddable resources and add them to your Assembly.info.cs file.

Abs
+3  A: 

I've done this and it works. Create a placeholder for the controls:

<asp:PlaceHolder ID="WebGridPlaceholder" runat="server" >
</asp:PlaceHolder>

Then you can do this in CreateChildControls:

ScriptManager aSM = new ScriptManager();
aSM.ID = "GridScriptManager";
WebGridPlaceholder.Controls.Add(aSM);
daduffer
A: 

I had this similar problem and found the best way was to add a global ScriptManager to the masterpage then in the code behind you can add to it by:

ScriptManager.GetCurrent(Page).Services.Add(new ServiceReference(virtualPath));
d1k_is
A: 

I ran into this problem with a custom ascx server control. I tried many solutions involving adding script to the OnInit events of the control (which doesn't get executed until after it checks for the ScriptManager control), adding logic inside of server tags on the control, and adding things to about every other event. No good. I finally built a control that inherits from ScriptManagerProxy and then uses ktrauberman's piece of code, slightly modified, to add a ScriptManager if needed:

  public class ProxiedScriptManager : ScriptManagerProxy
  {
    protected override void OnInit(EventArgs e)
    {
      //double check for script-manager, if one doesn't exist, 
      //then create one and add it to the page
      if (ScriptManager.GetCurrent(this.Page) == null)
      {
        ScriptManager sManager = new ScriptManager();
        sManager.ID = "sManager_" + DateTime.Now.Ticks;
        Controls.AddAt(0, sManager);
      }

      base.OnInit(e);
    }
  }

That did it for me.

Josh
A: 

I had the same basic issue the rest of you had. I was creating a custom ascx control and wanted to be able to not worry about whether or not the calling page had the scriptmanager declared. I got around the issues by adding the following to the ascx contorl itself.

to the ascx page -

<asp:PlaceHolder runat="server" ID="phScriptManager"></asp:PlaceHolder>

in the update panel itself - oninit="updatePanel1_Init"

to the ascx.cs file -

protected void updatePanel1_Init(object sender, EventArgs e)
{
     if (ScriptManager.GetCurrent(this.Page) == null)
     {
         ScriptManager sManager = new ScriptManager();
         sManager.ID = "sManager_" + DateTime.Now.Ticks;
         phScriptManager.Controls.AddAt(0, sManager);
     }
}

Thank you to everyone else in this thread who got me started.

Jon