Reputation: 47
I'm trying to add Google Adword conversion code script to certain aspx pages in our website but I'm running into an issue with the site using master pages. The Google instruction said to place the code before the body tag but with master pages being used the code will be on all the pages using the master page. I would like setup it up where certain pages use individual conversion codes with others not using anything. Any suggestions or examples would be appreciated. Also, I'm using C#.
Jamal
Upvotes: 2
Views: 1432
Reputation: 733
There are lots of different ways to communicate with controls on Master pages from individual pages. One of them is to create some simple custom controls and use the same pattern .NET uses with it's ScriptManager/ScriptManagerProxy controls. Basically, you put can a ScriptManager control on a Master page with default settings, then if you need to override the defaults on a page, you use a ScriptManagerProxy control.
I don't really know all that's involved with Adwords conversion code, but you could create the custom controls something like this:
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;
}
}
}
AdwordConversionProxy Control:
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;
}
}
}
}
Then you would just put an AdwordConversion control on your master page with default values, and you would put AdwordConversionProxy controls on the individual pages that needed their own settings.
Upvotes: 1