views:

393

answers:

3

Hi,

I design an ASP.NET web usercontrol and with a maskeditor and scriptmanager, I always get an object reference not set to an instance of an object exception at runtime.

Stacktrace is:

[InvalidOperationException: Only one instance of a ScriptManager can be added to the page.] System.Web.UI.ScriptManager.OnInit(EventArgs e) +384613 System.Web.UI.Control.InitRecursive(Control namingContainer) +333 System.Web.UI.Control.InitRecursive(Control namingContainer) +210 System.Web.UI.Control.InitRecursive(Control namingContainer) +210 System.Web.UI.Control.InitRecursive(Control namingContainer) +210 System.Web.UI.Control.InitRecursive(Control namingContainer) +210 System.Web.UI.Control.InitRecursive(Control namingContainer) +210 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +378

What causes this? Thanks

A: 

I think your error is more related with "Only one instance of a ScriptManager can be added to the page".

Check if you have more than one ScriptManager on your page

Elph
Apologies, yea the current except is actually exactly that but if I taker the scriptmanager out, I get "object reference not set to an instance of an object" error.
dotnetdev
Ok, maybe you're having problems with master and content pages as @Zaph comments below?
Elph
A: 

Where are you declaring the ScriptManager - in the UserControl, the Page that host the control, or in a MasterPage?

I'd recommend (if you can - the main caveat is .NET 3.5 installed on the server) defining the ScriptManager in the MasterPage (or page level if you're not using MasterPages), and then using a ScriptManagerProxy in your user control:

Enables nested components such as content pages and user controls to add script and service references to pages when a ScriptManager control is already defined in a parent element.

Edit to add:

If you can't use the ScriptManagerProxy, then either take a look at the ToolkitScriptManager from the AJAX control toolkit - it gives you a lot of the features of the .NET 3.5 ScriptManager without having to use 3.5, including the .GetCurrent method:

ScriptManager scriptManager = ToolkitScriptManager.GetCurrent(Page);

if (null != scriptManager) {
  // Create a new ToolkitScriptManager and add it to the page.
}

Alternatively you could perform that lookup yourself, by iterating through the Page's control collection looking for a ScriptManager instance.

Zhaph - Ben Duguid
A: 

My code is now as follows - UserCtrl:

<body style="text-align: center">

<form runat="server">

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
 <ContentTemplate>
    ... Table is here.

And the page:

<form id="form1" runat="server">
<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

   <uc1:RackRecable runat="server"   />
</div>
</form>

Is this correct? I still get the exception.

dotnetdev
Really, you should edit your question to include this, as it's not "an answer".No, it's not right: You shouldn't have either the body element or the form element in the user control (it's not valid HTML to have nested bodies or forms, trying to have two `form runat="server"` will cause things to break) - what is the exact error you are getting - if you've not updated the reference to the ScriptManagerProxy you'll still have an NullReferenceException.
Zhaph - Ben Duguid