[英]Render Macro control on page load in umbraco template
I am trying to load the razor script which exists as a separate macro in umbraco. 我正在尝试加载作为umbraco中的单独宏存在的剃刀脚本。 I am using umbraco 4.7.0
我正在使用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 谢谢
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 . 注意:
FindControlRecursive()
方法来自此论坛线程 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.