[英]ASP.NET / DotNetNuke - Prepend HTML to the body from of a User Control
I was trying to add HTML (specifically the Google Tag Manager snippet), right after the opening body tag, from a ASP.NET user control / DotNetNuke Skin in code behind. 我试图在开放的正文标记之后添加HTML(特别是Google跟踪代码管理器代码段),后面是代码隐藏的ASP.NET用户控件/ DotNetNuke Skin 。
I tried the following code: 我尝试了以下代码:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Page.PreLoad += new EventHandler(Page_PreLoad);
}
protected void Page_PreLoad(object sender, EventArgs e)
{
HtmlGenericControl body = (HtmlGenericControl) Page.FindControl("ctl00$body");
body.Controls.AddAt(0, new Literal() {ID = "GoogleTagManager", Text = "(...)" };
}
This worked so far; 这到目前为止工作; the HTML appeared at the desired location.
HTML出现在所需的位置。 But I noticed that whenever the code was executed, following PostBacks seemed not to be processed.
但我注意到,每当代码执行时, PostBacks似乎都没有被处理。
I can't put the HTML into the .aspx page because we have multiple DotNetNuke portals where we don't want to have the same snippet. 我无法将HTML放入.aspx页面,因为我们有多个DotNetNuke门户网站,我们不希望拥有相同的代码段。
Does anyone know a solution how to achieve this, while keeping the postbacks working? 有没有人知道如何实现这一目标的解决方案,同时保持回发工作?
__ __
Thank you and greetings from Switzerland 谢谢你和瑞士的问候
Raphael 拉斐尔
You might be able to use the client resource manager placeholders for this. 您可以使用客户端资源管理器占位符。
http://www.dnnsoftware.com/wiki/Page/Client-Resource-Management-API http://www.dnnsoftware.com/wiki/Page/Client-Resource-Management-API
Specifically, in the default.aspx file of your site you'll see a BodySCRIPTS placeholder. 具体来说,在您网站的default.aspx文件中,您将看到一个BodySCRIPTS占位符。
That or if you want, you might be able to target the Body ID element by getting to the Page base, I do something like that for controlling the Title of a page in https://dnnsimplearticle.codeplex.com/SourceControl/latest#cs/Controls/ArticleView.ascx.cs 如果您愿意,您可以通过访问Page base来定位Body ID元素,我可以通过https://dnnsimplearticle.codeplex.com/SourceControl/latest#来控制页面标题。 CS /控制/ ArticleView.ascx.cs
var tp = (CDefault)Page;
tp.Title = curArticle.Title;
You can modify SiteAnalytics.config file under httpdocs and adapt: 您可以在httpdocs下修改SiteAnalytics.config文件并进行调整:
Body True 身体真实
This will by applied to the portals who have a Google control under Admin > Google Analytics 这将应用于在“管理”>“Google Analytics”下拥有Google控件的门户网站
But i don't know why, the postbacks don't work with this solution. 但我不知道为什么, 回发不适用于这个解决方案。
Do you have a solution for this? 你有解决方案吗? Is the DNN incompatible with GTM ?
DNN与GTM不兼容吗?
Just found out that the Google Tag Manager script can be added after the body tag and this can be found in default.ascx 刚刚发现可以在body标签之后添加Google跟踪代码管理器脚本,这可以在default.ascx中找到
<%@ Page Language="vb" AutoEventWireup="false" Explicit="True" Inherits="DotNetNuke.Framework.DefaultPage" CodeFile="Default.aspx.vb" %>
<%@ Register TagPrefix="dnn" Namespace="DotNetNuke.Common.Controls" Assembly="DotNetNuke" %>
<asp:literal id="skinDocType" runat="server"></asp:literal>
<html<%= HtmlAttributeList %>>
<head id="Head" runat="server">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<meta content="text/javascript" http-equiv="Content-Script-Type"/>
<meta content="text/css" http-equiv="Content-Style-Type"/>
<meta id="MetaRefresh" runat="Server" http-equiv="Refresh" name="Refresh" />
<meta id="MetaDescription" runat="Server" name="DESCRIPTION" />
<meta id="MetaKeywords" runat="Server" name="KEYWORDS" />
<meta id="MetaCopyright" runat="Server" name="COPYRIGHT" />
<meta id="MetaGenerator" runat="Server" name="GENERATOR" />
<meta id="MetaAuthor" runat="Server" name="AUTHOR" />
<meta name="RESOURCE-TYPE" content="DOCUMENT" />
<meta name="DISTRIBUTION" content="GLOBAL" />
<meta id="MetaRobots" runat="server" name="ROBOTS" />
<meta name="REVISIT-AFTER" content="1 DAYS" />
<meta name="RATING" content="GENERAL" />
<meta http-equiv="PAGE-ENTER" content="RevealTrans(Duration=0,Transition=1)" />
<link href="StyleSheets/LPStyles.css" type="text/css" rel="stylesheet">
<style type="text/css" id="StylePlaceholder" runat="server"></style>
<asp:placeholder id="CSS" runat="server" />
</head>
<body id="Body" runat="server" >
<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=XXX-XXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-TK4KKM');</script>
<!-- End Google Tag Manager -->
<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=XXX-XXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','XXX-XXXXXX');</script>
<!-- End Google Tag Manager -->
<dnn:Form id="Form" runat="server" ENCTYPE="multipart/form-data" >
<asp:Label ID="SkinError" runat="server" CssClass="NormalRed" Visible="False"></asp:Label>
<asp:PlaceHolder ID="SkinPlaceHolder" runat="server" />
<input id="ScrollTop" runat="server" name="ScrollTop" type="hidden" />
<input id="__dnnVariable" runat="server" name="__dnnVariable" type="hidden" />
</dnn:Form>
</body>
</html>
I am trying to add GTM to a DNN portal. 我正在尝试将GTM添加到DNN门户。 I've Read through a few post about this but haven't been able to come up with a solution.
我已阅读了一些关于此的帖子,但未能提出解决方案。 I understand you can add to the default.aspx page but in my case it must be added through a skin package.
我知道你可以添加到default.aspx页面,但在我的情况下,它必须通过皮肤包添加。
I've tried using a cs or VB page but can't get it to add a control to the Body tag. 我尝试过使用cs或VB页面,但是无法将它添加到Body标签中。
I can add a attribute to the body tag ok but trying to add a controller always fails. 我可以在body标签中添加一个属性ok,但尝试添加控制器总是失败。 @rapael you mentioned "var tp = (CDefault)Page; tp.FindControl("Body").Controls.AddAt(0, new Literal() { Text = "" }); "
@rapael你提到“var tp =(CDefault)Page; tp.FindControl(”Body“)。Controls.AddAt(0,new Literal(){Text =”“});”
can you provide some more details as to how you implemented? 你能提供一些关于你如何实施的更多细节吗? Thanks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.