C# Google Adword和asp.net母版页

C# Google Adword和asp.net母版页,c#,asp.net,google-ads-api,C#,Asp.net,Google Ads Api,我试图添加谷歌Adword转换代码脚本到我们网站的某些aspx页面,但我遇到了使用母版页的网站问题。谷歌的指令说,将代码放在body标签之前,但如果使用母版页,则代码将出现在使用母版页的所有页面上。我想设置它在某些网页使用个别转换代码与其他不使用任何东西。如有任何建议或示例,将不胜感激。还有,我用的是C Jamal有许多不同的方法可以从各个页面与母版页上的控件进行通信。其中之一是创建一些简单的自定义控件,并使用.NET与其ScriptManager/ScriptManagerProxy控件使用的

我试图添加谷歌Adword转换代码脚本到我们网站的某些aspx页面,但我遇到了使用母版页的网站问题。谷歌的指令说,将代码放在body标签之前,但如果使用母版页,则代码将出现在使用母版页的所有页面上。我想设置它在某些网页使用个别转换代码与其他不使用任何东西。如有任何建议或示例,将不胜感激。还有,我用的是C


Jamal

有许多不同的方法可以从各个页面与母版页上的控件进行通信。其中之一是创建一些简单的自定义控件,并使用.NET与其ScriptManager/ScriptManagerProxy控件使用的相同模式。基本上,可以使用默认设置将ScriptManager控件放在母版页上,然后如果需要覆盖页面上的默认设置,可以使用ScriptManagerProxy控件

我真的不知道Adwords转换代码涉及的所有内容,但您可以创建如下自定义控件:

AdwordConversionControl:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SATest
{
    [DefaultProperty("ConversionCode")]
    [ToolboxData("<{0}:AdwordConversion runat=server></{0}:AdwordConversion>")]
    public class AdwordConversion : Control
    {
        private const string _conversionCodeKey = "cc";
        private const string _includeScriptKey  = "ic";

        [Category("Behavior")]
        [DefaultValue("")]
        public string ConversionCode
        {
            get { return (String)(ViewState[_conversionCodeKey] ?? "" ); }
            set { ViewState[_conversionCodeKey] = value; }
        }

        [Category("Behavior")]
        [DefaultValue(false)]
        public bool IncludeScript
        {
            get { return (bool)(ViewState[_includeScriptKey] ?? false ); }
            set { ViewState[_includeScriptKey] = value; }
        }


        protected override void Render(HtmlTextWriter writer)
        {
            if ( !IncludeScript ) { return; }

            string js = "<script type=\"text/javascript\">...Insert conversion code here: var code = " + ConversionCode + ";</script>";

            writer.Write( js );
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if ( Page.Items.Contains( typeof(AdwordConversion) ) ) 
            {
                throw new ApplicationException( "There can be only one AdwordConversion control defined on a page.  Use AdwordConversionProxy." );
            }

            Page.Items[typeof(AdwordConversion)] = this;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
名称空间测试
{
[默认属性(“转换代码”)]
[ToolboxData(“”)
公共类AdwordConversion:控件
{
私有常量字符串_conversioncodey=“cc”;
私有常量字符串_includeDescriptKey=“ic”;
[类别(“行为”)]
[默认值(“”)
公共字符串转换代码
{
获取{return(String)(ViewState[\u conversiondekey]??“”);}
设置{ViewState[\u conversioncodey]=value;}
}
[类别(“行为”)]
[默认值(false)]
公共图书馆
{
获取{return(bool)(ViewState[\u includeDescriptKey]?false);}
设置{ViewState[\u includeScriptKey]=value;}
}
受保护的覆盖无效渲染(HtmlTextWriter编写器)
{
如果(!IncludeScript){return;}
string js=“…在此处插入转换代码:var code=“+ConversionCode+”;”;
writer.Write(js);
}
受保护的覆盖无效OnInit(事件参数e)
{
碱基.奥尼特(e);
if(Page.Items.Contains(typeof(AdwordConversion)))
{
抛出新的ApplicationException(“一个页面上只能定义一个AdwordConversion控件。请使用AdwordConversionProxy”);
}
第页项目[类型(AdwordConversion)]=此项;
}
}
}
AdwordConversionProxy控件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SATest
{
    [DefaultProperty("ConversionCode")]
    [ToolboxData("<{0}:AdwordConversionProxy runat=server></{0}:AdwordConversionProxy>")]
    public class AdwordConversionProxy : Control
    {
        private string _conversionCode;
        private bool?  _includeScript;

        public string ConversionCode
        {
            get { return _conversionCode; }
            set { _conversionCode = value; }
        }

        public bool IncludeScript
        {
            get { return ( _includeScript.HasValue ) ? _includeScript.Value : false; }
            set { _includeScript = value; }
        }


        protected override void Render(HtmlTextWriter writer)
        {
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            AdwordConversion current = Page.Items[typeof(AdwordConversion)] as AdwordConversion;

            if ( current == null )
            {
                throw new ApplicationException( "AdwordConversionProxy requires that an AdwordConversion control already exist on a page." );
            }

            if ( _conversionCode != null )
            {
                current.ConversionCode = _conversionCode;
            }

            if ( _includeScript.HasValue )
            {
                current.IncludeScript = _includeScript.Value;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
名称空间测试
{
[默认属性(“转换代码”)]
[ToolboxData(“”)
公共类AdwordConversionProxy:控件
{
私有字符串_转换代码;
私人住宅?包括描述;
公共字符串转换代码
{
获取{return\u conversionCode;}
设置{u conversionCode=value;}
}
公共图书馆
{
获取{return(_includeScript.HasValue)?_includeScript.Value:false;}
设置{u includeScript=value;}
}
受保护的覆盖无效渲染(HtmlTextWriter编写器)
{
}
受保护的覆盖无效OnPreRender(EventArgs e)
{
基于预渲染(e);
AdwordConversion当前=页项[typeof(AdwordConversion)]作为AdwordConversion;
如果(当前==null)
{
抛出新的ApplicationException(“AdwordConversionProxy要求页面上已经存在AdwordConversion控件。”);
}
如果(_conversionCode!=null)
{
current.ConversionCode=\u ConversionCode;
}
如果(_includeScript.HasValue)
{
current.IncludeScript=\u IncludeScript.Value;
}
}
}
}

然后,您只需在母版页上放置一个带有默认值的AdwordConversion控件,然后在需要自己设置的各个页面上放置AdwordConversion代理控件。

实际上,我没有意识到我必须做些什么。我会纠正的。我很抱歉没有注意。仅供参考……我已经纠正了我的错误。我确实认为这是最好的答案之一。更新可能需要一段时间。我会尝试一下并让您知道。我在网上找到的是:。如果你能看看它,告诉我你的想法。那样也行。。。如果希望在不同的页面上使用不同的转换代码,则必须添加额外的变量。这与上面的主要区别在于它需要您在代码中设置变量,而上面的解决方案允许您在aspx中设置变量。另外,另一种解决方案不能跨多个母版页重复使用。如果你只有一个,这可能并不重要。祝你好运