Using Server-Side IFRAMES to show Ads

By Peter Bromberg

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.

Popularity  (4338 Views)
Picture
Biography - Peter Bromberg
Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking, financial and telephony for over 20 years. Pete focuses exclusively on the .NET Platform, and currently develops SOA and other .NET applications for a Fortune 500 clientele. Peter enjoys producing digital photo collage with Maya,playing jazz flute, the beach, and fine wines. You can view Peter's UnBlog and IttyUrl sites. Follow Microsoft MVP
Create New Account
Article Discussion: Using Server-Side IFRAMES to show Ads
Peter Bromberg posted at Thursday, November 22, 2007 11:05 AM
src path
geetha Naidu replied to Peter Bromberg at Thursday, November 22, 2007 11:05 AM
Does the file "txtUrl.Text" resides in the application directory or other directory...