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:
The PageInit is executed without problems, but replacing script manager this way causes an exception later on.
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:
Stack Trace:
Already a day spent fighting with it, but no success.
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.