Friday, May 14, 2010

AJAX in SharePoint - Tidbits

Most of us know what an AJAX is in the world of annoying web page flckering. So I dont want to explain here about it. Instead, I wanted to give some information/things to remember about implementing AJAX pages in SharePoint.

  1. Make sure controls are properly added to UpdatePanel while adding controls in CreateChildControls method.
  2. Make sure to call the following fix up script in CreateChildControls method at the very begining
    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(ITTPAJAXWebPart), "UpdatePanelFixup",
    fixupScript, true);
    }
    }
    

  3. Make sure to have the following function in the web part code. This will ensure to keep ScriptManager control at the top of the page if it is not found already on the master page.

    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    
    //get the existing ScriptManager if it exists on the page
    ScriptManager _AjaxManager = ScriptManager.GetCurrent(this.Page);
    if (_AjaxManager == null)
    {
    ////create new ScriptManager and EnablePartialRendering
    _AjaxManager = new ScriptManager();
    _AjaxManager.EnablePartialRendering = true;
    
    //Fix problem with postbacks and form actions (DevDiv 55525)
    Page.ClientScript.RegisterStartupScript(this.GetType(), this.ID, "_spOriginalFormAction = document.forms[0].action;", true);
    
    if ((this.Page.Form != null))
    {
    string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
    if (!string.IsNullOrEmpty(formOnSubmitAtt) & formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
    {
    this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
    }
    this.Page.Form.Controls.AddAt(0, _AjaxManager);
    }
    }
    }
    

  4. Though optional, make sure to have UpdateProgress template control used so that user is informed about what is going on inside the operation. Loading image/icon will be ideal to do this

  5. Make sure all the configurations related to ASP.NET AJAX are properly extended in the application's web.config file. Be particularly sure about the version. Since 3.5 is the current version of .NET Framework as of this writing (though 4.0 just released about a month ago), make sure the 4-part assembly information reflects correct .NET Framework version.



Technorati Tags: ,,,

No comments:

Post a Comment