0

I have a script based on .NET & Razor that renders on a webpage immediately, before any html elements have been rendered. Apart from a MySQL query, the script is solely writing some of the data onscreen using:

Response.Write("Some data");

I'm using Umbraco v4.7.2 and would like the script to render where I inserted the macro, after the whole document has loaded. I've tried wrapping my script in an event handler, similar to the following, but this isn't recognised and causes the whole script to render as text:

protected void Page_LoadComplete(object sender, EventArgs e)  {
    Response.Write("Page Load Complete!");
}

Event handlers seem to be used frequently but I don't quite understand the Umbraco documentation, is there something I need to reference in my razor script in order for them to render? Is there a 'cs' file I need to reference?

I've also tried using the 'RenderEvent' attribute, which based on my script doesn't appear to do anything.

mmmoustache
  • 2,273
  • 6
  • 41
  • 62
  • 1
    Unfortunately, I don't know enough about Razor to answer with any confidence. With traditional WebForms, I would suggested using Literals to place your code. But is this answer of any use? - [How to use Razor like asp:Literal?](http://stackoverflow.com/questions/5536209/how-to-use-razor-like-aspliteral) – Goran Mottram Jun 28 '12 at 12:47
  • Thanks for the suggestion, this looks like it can help me out! – mmmoustache Jun 28 '12 at 13:28

2 Answers2

1

Umbraco events are used mainly when dealing with the API; when a document is saved, published, deleted, etc.

Using a different approach, you could build an Umbraco base class and then pull the results down with a Javascript AJAX call on load. Umbraco base allows you to quickly build REST extensions that allow you to access the Umbraco via the API.

Here's an example base class:

using umbraco.presentation.umbracobase;

namespace BaseExample
{
    [RestExtension("MyAlias")]
    public class TestClass
    {
        [RestExtensionMethod(allowAll = true)]
        public static string GetData()
        {
            return "Some data";
        }
    }
} 

More detailed instructions can be found in the Umbraco wiki. After you place the DLL for the base class into your bin directory, you can call the call the method by navigating to http://[domain]/base/MyAlias/GetData. By default the method will return XML, but to more easily integrate it with Javascript you can have it return JSON instead. Check out this SO answer for more details.

Community
  • 1
  • 1
Douglas Ludlow
  • 10,754
  • 6
  • 30
  • 54
0

I finally found out the simplest solution to this; using @Html.Raw() instead of Response.Write("Some data") did what I needed to do.

mmmoustache
  • 2,273
  • 6
  • 41
  • 62