1
votes

I am trying to load the razor script which exists as a separate macro in umbraco. I am using umbraco 4.7.0

I am basically doing geo-targeting and hence need to run the script each time on a page load. so I am looking for something like :

 <script type="c#" runat="server">
protected void Page_Load(object sender, EventArgs e)
  {
    Call the Macro here
  }

Obviously I can do this in the template

 <umbraco:Macro Alias="GeoTargetting" runat="server" />

but I need to call this macro before any control is loaded in the page.

PLease help.

Thanks

1
Can you please tell what is the exact need to do this.. Whether to run any code in the macro page load?Mahesh KP
@mahesh : Thanks for it. The basic need is to avoid caching and load the macro content based on the url. What happens is, if I change the url of the page, it loads the page but the macro is cached due to which I am not able to get the exact contents in the macro. I have few conditions in the macro which needs to be invoked even if it's a postback.Aneesh
I am not sure what you meant by "macro is cached due to which I am not able to get the exact contents in the macro". If you want to do things in all request you can use an httpHandler which will override all the page life cycle events. So all the page events will be routed through this handler events.Mahesh KP
@aneesh, why don't you just disable caching on the macro?Douglas Ludlow

1 Answers

0
votes

This appears to work alright:

Template:

<umbraco:Macro Alias="Sandbox" runat="server"></umbraco:Macro>
<asp:Label runat="server" ID="Label1" Text="Yo"></asp:Label>

Macro Script:

@using System.Web.UI.WebControls
@using umbraco.MacroEngines
@inherits DynamicNodeContext
@{
    Page page = HttpContext.Current.Handler as Page;
    page.Load += new EventHandler(page_Load);
}

@functions
{
    static void page_Load(object sender, EventArgs e)
    {
        Page page = sender as Page;
        Label label = FindControlRecursive(page, "Label1") as Label;
        label.Text = "Hello";
    }

    private static Control FindControlRecursive(Control Root, string Id)
    {
        if (Root.ID == Id)
            return Root;

        foreach (Control Ctl in Root.Controls)
        {
            Control FoundCtl = FindControlRecursive(Ctl, Id);

            if (FoundCtl != null)
                return FoundCtl;
        }

        return null;
    }
}

Note: The FindControlRecursive() method is from this forum thread.