Using Server-Side IFRAMES to show Ads


By Peter Bromberg
Printer Friendly Version
  

IFRAME is a very flexible HTML control, but many ASP.NET developers aren't aware that you can turn it into an ASP.NET control by simply adding the runat="server" attribute. Provided that you cast this to the HtmlGenericControl("IFRAME") type, you can manipulate virtually everything about your IFRAME with server - side code.



This short article shows how you can turn an ASCX UserControl into one of these IFRAMES and use it to show ads (or whatever else suits your fancy).

First, we need to create a UserControl. Here is the ASCX Markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AdControl.ascx.cs" Inherits="IFrameAds.AdControl" %>
 <iframe id="AdFrame" runat="server" width="468px" height="160px" frameborder="no" scrolling ="no" src="Altads.aspx"></iframe>

As can be seen above, all we need is our IFRAME on the control designer with the runat="server" attribute, and sufficient additional attributes to get the default display characteristics that we want.  The "src" attribute is initially set to a simple script-only page that inserts random ads. This is specified also by some sample Google Adsense code I have in the AppSettings section of the web.config. Now here is the codebehind, which includes a custom "PlaceAd" method:

using System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;

namespace IFrameAds
{
    public partial class AdControl : System.Web.UI.UserControl
      {
       
public HtmlGenericControl AdControl1 = new HtmlGenericControl("IFRAME");
             
protected void Page_Load(object sender, EventArgs e)
                   {
                      AdControl1 = (
HtmlGenericControl)this.FindControl("AdFrame");
                   }

public void PlaceAd(string width, string height, string src, string style, string adCode)
     {
        AdControl1.Attributes[
"height"] = height;
       AdControl1.Attributes[
"width"] = width;
      AdControl1.Attributes[
"style"] = style;
      // use one or the other of these ideas - either pass src, or pass adCode parameter:
      if (src != null)
         AdControl1.Attributes[
"src"] = src;
    
else
        AdControl1.InnerHtml = adCode;
    }
  }
}

So with our control dragged onto a Page, we can tell it how big to be, and even set style attributes such as "float:left;" and so on, which makes it very flexible.To use the control on a page, all we need to do is drag it on from the Solution Explorer. In my sample ASPX page, I have code in Page_Load that places an ad, and I also have a button with a textbox that let's you see how the src property can be changed programmatically at runtime:


protected void Page_Load(object sender, EventArgs e)
{
AdControl1.PlaceAd(
"468px", "260px", null, "float:right", ConfigurationManager.AppSettings["adCode160X240Left"]);
}

protected void Button1_Click(object sender, EventArgs e)
{
AdControl1.PlaceAd(
"468px", "160px", txtUrl.Text, "float:left", null);
}

 

You can download the sample Visual Studio 2005 Solution here.



Biography
Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking ,financial and telephony for 20 years. Pete focuses exclusively on the .NET Platform, and his samples at GotDotNet.com have been downloaded over 56,000 times. Peter enjoys producing 3D raytraced digital photo collage with Maya, the beach, and fine wines. You can view Peter's UnBlogIttyUrl, and BlogMetafinder sites.
Please post questions at forums, not via email!

button
 
Article Discussion: Using Server-Side IFRAMES to show Ads
  Peter Bromberg posted at 22-Nov-07 11:05
Original Article

 
  src path
  geetha Naidu replied to Peter Bromberg at 29-May-08 05:22
Does the file "txtUrl.Text" resides in the application directory or other directory...