This is a migrated thread and some comments may be shown as answers.
ScriptRegistrar.OnDocumentReady in RenderAction
4 Answers 290 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kelly Stuard
Top achievements
Rank 1
Kelly Stuard asked on 26 Jan 2010, 08:28 PM
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:
<%  
Html.Telerik().ScriptRegistrar()  
    .OnDocumentReady(() => { %> 
        alert('hi from Site.Master');  
    <% });  
%> 
 
<% Html.RenderPartial("LogOnUserControl"); %> 
<% Html.RenderAction("LogOnUserControl"); %> 
 
<%=Html.Telerik().ScriptRegistrar() %> 
Index.aspx:
<%  
Html.Telerik().ScriptRegistrar()  
    .OnDocumentReady(() => { %> 
        alert('hi from Index.aspx');  
    <% });  
%> 
LogOnUserControl.ascx:
<%  
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.

4 Answers, 1 is accepted

Sort by
0
Kelly Stuard
Top achievements
Rank 1
answered on 28 Jan 2010, 09:04 PM
Is there any additional information I can provide to help explain or replicate the issue I am having? Can anyone else reproduce this issue using the instructions?
0
Accepted
Atanas Korchev
Telerik team
answered on 29 Jan 2010, 10:01 AM
Hi Kelly Stuard,

I've managed to reproduce the problem. Unfortunately I don't think we can fix it easily due to the implementation of RenderAction and the specifics of the ScriptRegistrar.

The reason for the exception is that the Action which is supposed to output the script is executed out of scope and the TextWriter it is using is already disposed. This disposal occurs because of RenderAction and is beyond our control.

Probably making an overload of OnDocumentReady which accepts a string will work but I don't think it will be very convenient. Here is what I mean:

<%  
Html.Telerik().ScriptRegistrar()  
    .OnDocumentReady("alert('hi')");
);  
%> 

Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Kelly Stuard
Top achievements
Rank 1
answered on 29 Jan 2010, 03:40 PM
Unfortunately, that's the same conclusion I came to after looking at what was being done.

Thank you for doing the research.
0
Kelly Stuard
Top achievements
Rank 1
answered on 02 Mar 2010, 05:47 PM
It seems that in the latest code release you guys did add an overload that accepts a string, like you were talking about above.

Tags
General Discussions
Asked by
Kelly Stuard
Top achievements
Rank 1
Answers by
Kelly Stuard
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or