Webpart and AJAX – tricks on creating ScriptManager

ScriptManager object is required if you want to use AJAX (Microsoft Atlas) in your webpart. In a ASPX page, there should only be one instance of ScriptManager object. However as just a component of a frame work/page, your webpart has no knowledge if the main page (or other web parts) has created the ScriptManager or not. There are 2 options.

One options is to modify the master page to create the ScriptManager object declaratively. Open \12\TEMPLATE\GLOBAL\default.master (or locate it by <site url>/_catalogs/masterpage). Add the following into the markup of your page. A recommended location is right beneath the WebPartManager registration (<WebPartPages:SPWebPartManager id=”m” runat=”Server” />, line 25 in above file):

<asp:ScriptManager runat=”server” ID=”ScriptManager1″ EnablePartialRendering=”True”></asp:ScriptManager>

However, replying on master page to create the object is not very convinient considering deployment and future maintenance. You have to deploy the master page together with your web part, and if there is any design change and new master page is used, you code will break if above script is forgot in the new master page.

So I recommend dynamically create the ScriptManager in your own web part. Then you face 2 issues:

  1. What if the page already has, or another web part already created one?
  2. For Get and Post request, the event sequence is different. How to make sure the ScriptManager is created in the right order?

The first issue can be addressed by putting your creation part in a try catch blog – of course you are running risk of ignoring legitimate exceptions. To address the second problem, you can check if there is already such object in the Controls collection. I have a AddScriptManager():

protected void AddScriptManager()
{
try
{
if (Controls.IndexOf(scriptManager) < 0)
{
scriptManager.EnablePartialRendering = true;
Controls.Add(scriptManager);
}
}
catch { }
}

(The scriptManager object is created beforehand.)

Advertisements
This entry was posted in Webpart and tagged , . Bookmark the permalink.

7 Responses to Webpart and AJAX – tricks on creating ScriptManager

  1. Pixie says:

    This is the only solution that worked for me.

    protected override void OnInit(EventArgs e)
    {

    if (ScriptManager.GetCurrent(this.Page) == null)

    {
    scriptManager = new ScriptManager();
    scriptManager.ID = “myScriptManager”;
    scriptManager.EnablePartialRendering = true;
    this.Page.Form.Controls.Add(scriptManager);

    }

    base.OnInit(e);
    }

  2. Sandeep says:

    Hi

    I got following errors after doing so.

    System.Web.HttpException: The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
    at System.Web.UI.ControlCollection.Add(Control child)

  3. Miro says:

    Hi
    Thanks for the great post here.
    but im wondering, we all know that webparts might be a usual asp.net user control , what if im develepoing a user control and want to deploy it on a sharepoint site , does the above applies??

    I mean , can i add script manager to a user control instead of a regular page??

    Thanks in Advance,

  4. Dilip says:

    Hi Miro,

    Did you get any solution. i have the same problem.. please let me know if you have fix around.

    Thank,
    Dilip

  5. Rahul says:

    Hello,
    i have error when i add script manager on my webpart thay give error
    “The Name scriptManager does not exist in the current context”
    and when i add scriptManager on master page thay give error:
    “A IE6 the tag is not permitted”
    Plz and one help me
    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s