search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsFAQs
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Operating SystemsArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

Using Server-Side IFRAMES to show Ads


By Peter Bromberg
Printer Friendly Version
View My Articles
40 Views
    

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
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.
Please post questions at forums, not via email!

button
Article Discussion: Using Server-Side IFRAMES to show Ads
Peter Bromberg posted at Thursday, November 22, 2007 11:05 AM
Original Article
 

src path
geetha Naidu replied to Peter Bromberg at Thursday, May 29, 2008 5:22 AM
Does the file "txtUrl.Text" resides in the application directory or other directory...