This is a migrated thread and some comments may be shown as answers.

Replacing ScriptManager with RadScriptManager from WebPart dynamically - ERROR !

3 Answers 300 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Ross
Top achievements
Rank 1
Ross asked on 30 Sep 2010, 10:08 AM
Hello,

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)
{
    base.OnInit(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))
        {
            Page.Form.Controls.Remove(ctr);
            break;
        }
    }
    Page.Items.Remove(typeof(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.



3 Answers, 1 is accepted

Sort by
0
T. Tsonev
Telerik team
answered on 06 Oct 2010, 09:56 AM
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:

Page.Items.Remove(typeof(ScriptManager));
 
RadScriptManager rsm = new RadScriptManager();
Page.Form.Controls.Add(rsm);

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
0
Dasha
Top achievements
Rank 1
answered on 17 Feb 2012, 02:47 PM
Hello,

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,

Dasha.
0
Cat Cheshire
Top achievements
Rank 1
answered on 22 Feb 2012, 02:49 PM
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
p2p.wrox.com:  Script Combining
I am not sure that those solutions will work under SharePoint, but you can try them and post your questions there.
Tags
Ajax
Asked by
Ross
Top achievements
Rank 1
Answers by
T. Tsonev
Telerik team
Dasha
Top achievements
Rank 1
Cat Cheshire
Top achievements
Rank 1
Share this question
or