This question is locked. New answers and comments are not allowed.
I'm encountering an error using the OnDocumentReady functionality. To reproduce, make a new default MVC application and add the following to the end of each of the files already there.
Site.Master:
Index.aspx:
LogOnUserControl.ascx:
If I remove the line "<% Html.RenderAction("LogOnUserControl"); %>" then everything works as expected. However, with that line I get the following server error:
I think this is happening because by the time we get to the render of the script manager, the OnDocumentReady action is set to render to the (now-closed) child action instead of the current TextWriter.
Site.Master:
| <% |
| Html.Telerik().ScriptRegistrar() |
| .OnDocumentReady(() => { %> |
| alert('hi from Site.Master'); |
| <% }); |
| %> |
| <% Html.RenderPartial("LogOnUserControl"); %> |
| <% Html.RenderAction("LogOnUserControl"); %> |
| <%=Html.Telerik().ScriptRegistrar() %> |
| <% |
| Html.Telerik().ScriptRegistrar() |
| .OnDocumentReady(() => { %> |
| alert('hi from Index.aspx'); |
| <% }); |
| %> |
| <% |
| Html.Telerik().ScriptRegistrar() |
| .OnDocumentReady(() => { %> |
| alert('hi from LogOnUserControl.ascx'); |
| <% }); |
| %> |
If I remove the line "<% Html.RenderAction("LogOnUserControl"); %>" then everything works as expected. However, with that line I get the following server error:
| Server Error in '/' Application. |
| -------------------------------------------------------------------------------- |
| Object reference not set to an instance of an object. |
| 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.NullReferenceException: Object reference not set to an instance of an object. |
| Source Error: |
| Line 2: <% |
| Line 3: Html.Telerik().ScriptRegistrar() |
| Line 4: .OnDocumentReady(() => { %> |
| Line 5: alert('hi from LogOnUserControl.ascx'); |
| Line 6: <% }); |
| Source File: c:\SourceCode\scratch\kstuard\MvcApplication2\MvcApplication2\Views\Shared\LogOnUserControl.ascx Line: 4 |
| Stack Trace: |
| [NullReferenceException: Object reference not set to an instance of an object.] |
| System.Web.Mvc.SwitchWriter.Write(String value) +8 |
| System.Web.UI.HtmlTextWriter.Write(String s) +27 |
| ASP.<>c__DisplayClass1.<__Render__control1>b__0() in c:\SourceCode\scratch\kstuard\MvcApplication2\MvcApplication2\Views\Shared\LogOnUserControl.ascx:4 |
| Telerik.Web.Mvc.UI.ScriptRegistrar.WriteScriptStatements(TextWriter writer) +1122 |
| Telerik.Web.Mvc.UI.ScriptRegistrar.Write(TextWriter writer) +49 |
| Telerik.Web.Mvc.UI.ScriptRegistrar.Render() +215 |
| Telerik.Web.Mvc.UI.ScriptRegistrarBuilder.Render() +34 |
| Telerik.Web.Mvc.UI.ScriptRegistrarBuilder.ToString() +28 |
| System.Web.HttpWriter.Write(Object obj) +22 |
| System.Web.Mvc.SwitchWriter.Write(Object value) +15 |
| System.Web.UI.HtmlTextWriter.Write(Object value) +31 |
| ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\SourceCode\scratch\kstuard\MvcApplication2\MvcApplication2\Views\Shared\Site.Master:46 |
| System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256 |
| System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19 |
| System.Web.UI.Control.Render(HtmlTextWriter writer) +10 |
| System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 |
| System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99 |
| System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 |
| System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134 |
| System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19 |
| System.Web.UI.Page.Render(HtmlTextWriter writer) +29 |
| System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59 |
| System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 |
| System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99 |
| System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 |
| System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266 |
| -------------------------------------------------------------------------------- |
| Version Information: Microsoft .NET Framework Version:2.0.50727.4005; ASP.NET Version:2.0.50727.4028 |
I think this is happening because by the time we get to the render of the script manager, the OnDocumentReady action is set to render to the (now-closed) child action instead of the current TextWriter.