Sue Hernandez's SharePoint Blog

SharePoint and Related Stuff

SharePoint 2007 Ajax ScriptManager with Layouts Application Page

So as you may already know, when creating Web Parts with SharePoint 2007, you create them for the most part in code, unless you use user controls, SmartPart, etc.  Adding a Script Manager is easy – you create it in CreateChildControls and THEN you add your UpdatePanel to the page.

But what if you want to use AJAX in an Application Page, that goes in the Layouts directory?  You don’t want to have to create the Update Panel programmatically, you want to be able to put it in Markup.  However, you need, absolutely MUST have, a ScriptManager as the first control in the page.

Yes, you could go and put it manually in your Master Page.  I don’t personally like that approach.

So here’s some code to put it in your Application page.  You have to actually SET the ScriptManager into the Items property of the Page (there is no ScriptManager.SetCurrent or the like).  I derived this by reading the blog here:  http://veskokolev.blogspot.com/2008/01/scriptmanager-in-basepage-class.html

namespace AjaxTextAppPage.SharePointRoot.Templage.Layouts
{
   public partial class AjaxApp : System.Web.UI.Page
   {
      protected TextBox txtInput;
      protected Button cmdClickMe;
      protected Label lblResults;
      protected UpdatePanel updPanel;

      protected ScriptManager mgr;

      public AjaxApp()
      {
         mgr = new ScriptManager;
         mgr.ID = "ScriptManager1";
         mgr.EnablePartialRendering = true;
         this.Page.Items[typeof(ScriptManager)] = mgr;
      }
      protected override void OnInit(EventArgs e)
      {
         base.OnInit(e);
         mgr = ScriptManager.GetCurrent(this.Page);
         if(mgr == null)
         {
            mgr = new ScriptManager;
            mgr.ID = "ScriptManager1";
            mgr.EnablePartialRendering = true;
            this.Page.Form.Controls.AddAt(0, mgr);
         }
      }
      protected void Page_Load(object sender, EventArgs e)
      {
         mgr = ScriptManager.GetCurrent(this.Page);
         if(mgr == null)
         {

            mgr.RegisterAsyncPostBackControl(cmdClickMe);

            EnsurePanelFix();
         }
      }
      protected void cmdClickMe_Click(object sender, EventArgs e)
      {
         lblResults.Text = "You typed '" + txtInput.Text + "'.";
      }
      private void EnsurePanelFix()
      {
         if (this.Page.Form != null)
         {
            string fixupScript = @"    _spBodyOnLoadFunctionNames.push(""_initFormActionAjax"");
               function _initFormActionAjax()   {  if (_spEscapedFormAction == document.forms[0].action) 
               { document.forms[0]._initialAction = document.forms[0].action;  } }
               var RestoreToOriginalFormActionCore = RestoreToOriginalFormAction;
               RestoreToOriginalFormAction = function()
               {  if (_spOriginalFormAction != null)  { RestoreToOriginalFormActionCore();
               document.forms[0]._initialAction = document.forms[0].action;  } }";

            ScriptManager.RegisterStartupScript(this, typeof(this), 
               "UpdatePanelFixup_MainMap", fixupScript, true);
            string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
            if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
            {
               this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
            }
            ScriptManager.RegisterStartupScript(this, typeof(this), "UpdatePanelFixup2_MainMap",
               "_spOriginalFormAction = document.forms[0].action;
               _spSuppressFormOnSubmitWrapper=true;", true);
         }
      }
   }
}

I’m not sure if you still need “EnsurePanelFix” (I supposedly needed this for web parts) and I’m not sure if you really need the OnInit, as you’re already adding the manager at instantiation.

This way, though, you can put your UpdatePanel markup right in the page and not have to add it programmatically.

Advertisements

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

%d bloggers like this: