Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 3.24 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Google Adword and asp.net master pages
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Web;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10.  
  11. namespace SATest
  12. {
  13.     [DefaultProperty("ConversionCode")]
  14.     [ToolboxData("<{0}:AdwordConversion runat=server></{0}:AdwordConversion>")]
  15.     public class AdwordConversion : Control
  16.     {
  17.         private const string _conversionCodeKey = "cc";
  18.         private const string _includeScriptKey  = "ic";
  19.  
  20.         [Category("Behavior")]
  21.         [DefaultValue("")]
  22.         public string ConversionCode
  23.         {
  24.             get { return (String)(ViewState[_conversionCodeKey] ?? "" ); }
  25.             set { ViewState[_conversionCodeKey] = value; }
  26.         }
  27.  
  28.         [Category("Behavior")]
  29.         [DefaultValue(false)]
  30.         public bool IncludeScript
  31.         {
  32.             get { return (bool)(ViewState[_includeScriptKey] ?? false ); }
  33.             set { ViewState[_includeScriptKey] = value; }
  34.         }
  35.  
  36.  
  37.         protected override void Render(HtmlTextWriter writer)
  38.         {
  39.             if ( !IncludeScript ) { return; }
  40.  
  41.             string js = "<script type="text/javascript">...Insert conversion code here: var code = " + ConversionCode + ";</script>";
  42.  
  43.             writer.Write( js );
  44.         }
  45.  
  46.         protected override void OnInit(EventArgs e)
  47.         {
  48.             base.OnInit(e);
  49.  
  50.             if ( Page.Items.Contains( typeof(AdwordConversion) ) )
  51.             {
  52.                 throw new ApplicationException( "There can be only one AdwordConversion control defined on a page.  Use AdwordConversionProxy." );
  53.             }
  54.  
  55.             Page.Items[typeof(AdwordConversion)] = this;
  56.         }
  57.     }
  58. }
  59.        
  60. using System;
  61. using System.Collections.Generic;
  62. using System.ComponentModel;
  63. using System.Linq;
  64. using System.Text;
  65. using System.Web;
  66. using System.Web.UI;
  67. using System.Web.UI.WebControls;
  68.  
  69. namespace SATest
  70. {
  71.     [DefaultProperty("ConversionCode")]
  72.     [ToolboxData("<{0}:AdwordConversionProxy runat=server></{0}:AdwordConversionProxy>")]
  73.     public class AdwordConversionProxy : Control
  74.     {
  75.         private string _conversionCode;
  76.         private bool?  _includeScript;
  77.  
  78.         public string ConversionCode
  79.         {
  80.             get { return _conversionCode; }
  81.             set { _conversionCode = value; }
  82.         }
  83.  
  84.         public bool IncludeScript
  85.         {
  86.             get { return ( _includeScript.HasValue ) ? _includeScript.Value : false; }
  87.             set { _includeScript = value; }
  88.         }
  89.  
  90.  
  91.         protected override void Render(HtmlTextWriter writer)
  92.         {
  93.         }
  94.  
  95.         protected override void OnPreRender(EventArgs e)
  96.         {
  97.             base.OnPreRender(e);
  98.  
  99.             AdwordConversion current = Page.Items[typeof(AdwordConversion)] as AdwordConversion;
  100.  
  101.             if ( current == null )
  102.             {
  103.                 throw new ApplicationException( "AdwordConversionProxy requires that an AdwordConversion control already exist on a page." );
  104.             }
  105.  
  106.             if ( _conversionCode != null )
  107.             {
  108.                 current.ConversionCode = _conversionCode;
  109.             }
  110.  
  111.             if ( _includeScript.HasValue )
  112.             {
  113.                 current.IncludeScript = _includeScript.Value;
  114.             }
  115.         }
  116.     }
  117. }