Replacing ScriptManager with RadScriptManager from WebPart dynamically - ERROR !

4 posts, 0 answers
  1. Ross
    Ross avatar
    3 posts
    Member since:
    Sep 2010

    Posted 30 Sep 2010 Link to this post


    We are developing webparts for Sharepoint 2010.  There is one strict rule we have to obey - not to modify default sharepoint master pages, because it would be very difficult to support this changes later on when webparts are distributed to clients. From the other hand we would like to modify the master page dynamically, adding RadScriptManager and RadAjaxManager controls from the OnInit event of the webpart.

    With RadAjaxManager there is no problem, but replacing the default ScriptManager with RadScriptManager causes a strange exception. To recreate the problem create a blank visual web part and create the OnInit override method like this:

    protected override void OnInit(EventArgs e)
        //this is a recomendation of Telerik, do not know if it necessary in Sharepoint 2010?
        Page.ClientScript.RegisterStartupScript(typeof(BlankWebPart), this.ID, "_spOriginalFormAction = document.forms[0].action;_spSuppressFormOnSubmitWrapper=true;", 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();";
        //Remove System.Web.UI.ScriptManager from Page.Form.Controls and Page.Items
        foreach (Control ctr in Page.Form.Controls)
            if (ctr.GetType() == typeof(System.Web.UI.ScriptManager))
        //Problematic code. If you remove it, no exception
        RadScriptManager rsm = new RadScriptManager();
        // rsm.ID = "RadScriptManager1";
        Page.Items.Add(rsm.GetType(), rsm);
        Page.Form.Controls.AddAt(0, rsm);
        //Adding Ajax manager, working ok
        RadAjaxManager ram = new RadAjaxManager();
        ram.ID = "RadAjaxManager1";
        Page.Items.Add(ram.GetType(), ram);
        Page.Form.Controls.AddAt(2, ram);

    The PageInit is executed without problems, but replacing script manager this way causes an exception later on.

    Page cannot be null. Please ensure that this operation is being performed in the context of an ASP.NET request.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: Page cannot be null. Please ensure that this operation is being performed in the context of an ASP.NET request.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [InvalidOperationException: Page cannot be null. Please ensure that this operation is being performed in the context of an ASP.NET request.]
       System.Web.UI.ScriptManager.get_IPage() +654824
       System.Web.UI.ScriptManager.OnPagePreRenderComplete(Object sender, EventArgs e) +59
       System.EventHandler.Invoke(Object sender, EventArgs e) +0
       System.Web.UI.Page.OnPreRenderComplete(EventArgs e) +11039454
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3672

    Already a day spent fighting with it, but no success.

  2. T. Tsonev
    T. Tsonev avatar
    2834 posts

    Posted 06 Oct 2010 Link to this post

    Hello Ross,

    Thank you for describing the problem in detail.

    The exception is caused by the original ScriptManager instance on the page. Although you removed it from the controls collection it still has event handlers hooked to the Page events. In this case the PagePreRenderComplete event handler is executed and it fails as the ScriptManager is no longer on the page.

    If you keep the old ScriptManager on the page you won't get an exception, but be warned that this is not a supported scenario and can cause unforeseen consequences. The current ScriptManager-related code can be replaced by:

    RadScriptManager rsm = new RadScriptManager();

    My recommendation is to replace the ScriptManager on the master page, if possible. You can turn off EnableScriptCombine on RadScriptManager in order to get the standard ScriptManager behavior. Then you can enable it on the pages where script combining is required.

    I hope this helps.

    Sincerely yours,
    Tsvetomir Tsonev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Dasha
    Dasha avatar
    81 posts
    Member since:
    Aug 2010

    Posted 17 Feb 2012 Link to this post


    We are trying to take advantage of the script combining capability, but we unfortunately cannot use RadScriptManager.  Is there any other possible workaround that you might suggest?

    Thank you very much,

  4. Cat Cheshire
    Cat Cheshire avatar
    128 posts
    Member since:
    Jun 2010

    Posted 22 Feb 2012 Link to this post

    You can take a look at the:
    Scott Hasenlman's Computer Zen Blog: ASP.NET Ajax - Script Combining and moving ScriptResource.axd's to Static Scripts
    or  Script Combining
    I am not sure that those solutions will work under SharePoint, but you can try them and post your questions there.
Back to Top