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:
- What if the page already has, or another web part already created one?
- 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.)