ASP.NET
  Home arrow ASP.NET arrow Page 4 - Programming an In-Text Advertising System ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Programming an In-Text Advertising System under ASP.NET 3.5
By: Xianzhong Zhu
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2009-01-21

    Table of Contents:
  • Programming an In-Text Advertising System under ASP.NET 3.5
  • The JavaScript files
  • The Server-side Programming
  • The Three Generic Handlers Related Programming

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Programming an In-Text Advertising System under ASP.NET 3.5 - The Three Generic Handlers Related Programming


    (Page 4 of 4 )

    Now, we’ll check out the three generic handlers one by one.

    AdArray.ashx

    The first handler, AdArray.ashx, is used to generate the ad data related array to be used on the client side:

    public class adArray : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

    context.Response.ContentType = "text/plain";//the output type

    string result="var Ads=new Array(";//specify the script variable to be returned

    Sql s =new Sql();

    System.Data .DataSet ds=s.getMyDataSet("select name from keyarray ");//obtain the ad keyword

    for(int i=0;i<ds.Tables[0].Rows.Count;i++)//append the obtained keyword to the string

    result += "'" + ds.Tables[0].Rows[i][0].ToString() + "',";

    if (result.EndsWith(","))//clear out the unwanted ',' character at the end of the string

    result = result.Substring(0,result.Length-1);

    result +=");";

    context.Response.Write(result);//output the script variable

    }

    public bool IsReusable {//not reusable

    get {

    return false;

    }

    }

    }

    As you’ve seen, the main function of the adArray handler searches an advertisement key word from the database, then generates an advertisement key word array according to the specified format, and finally returns the data to the client side. Next, let’s check out the second handler, AD.ashx. 

    AD.ashx

    The second handler, AD.ashx, will render the concrete ad contents related to some special ad keyword. In detail, it will query the corresponding ad contents from the tableaddefined in the AD.mdf database, and then return the data to the client side with some special format, which will finally be rendered on the client side. Below I've listed the associated code for the AD.ashx handler:

    public class AD : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

    context.Response.ContentType = "text/plain";

    string result = "";//the returned ad content

    Sql s = new Sql();

    string key = HttpContext.Current.Request.QueryString["key"];//the specified ad keyword

    string username = HttpContext.Current.Request.QueryString["username"];// the account of the website host who places the ad

    string str = "select description ,url from ad inner join keyarray on ad.keyid=keyarray.id where keyarray.name='"+key+"' order by ad.id desc";

    System.Data.DataSet ds = s.getMyDataSet(str);//obtain the ad content

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)//append all the relevant ad content to the string

    {

    result += "<a target="_blank" href="Redirect.ashx?username=" + username;

    result += "&url=" + ds.Tables[0].Rows[i]["url"].ToString() + "">" + ds.Tables[0].Rows[i]["description"].ToString() + "</a><br/>";

    }

    context.Response.Write(result);//output

    }

    public bool IsReusable {//not reusable

    get {

    return false;

    }

    }

    }

    Finally, let’s look at the third and last handler—redirect.ashx. As the name hints, this handler sees to record the hit count of the related ad, and also bears the responsibility of redirecting the current user to the website where the real ad-targeted contents exists. The following is the related code for the redirect.ashx handler:

    public class redirect : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

    string username = HttpContext.Current.Request.QueryString["username"];//the account of the website host who places the ad

    string url = HttpContext.Current.Request.QueryString["url"];//the url to redirect

    Sql s = new Sql();

    s.ExecuteSql("insert into hit(username,url)values('"+username+"','"+url +"')");//insert the ad data

    HttpContext.Current.Response.Redirect(url);//navigate the ad related website page

    }

    public bool IsReusable {//non-reusable

    get {

    return false;

    }

    }

    }

    -DOWNLOAD SOURCE-

    Summary

    In this installment, we mainly focused upon the core code programming of the in-text ad system. As you’ve seen, the real work is around the client side JavaScript programming. To gain a better understanding of this, you should first comprehend the invoking relationships between the script files. Then, we explored parts of the server side programming, such as the password encrypting, basic database operation encapsulation, and all three higher generic handlers, which are mainly leveraged to process and provide ad-related data to the client side scripts.

    In the third and last part of the in-text advertising system, we’ll shift to discussing the entire website owner- and administrator-related server-side programming and see how the items we discussed in this part will fit together. Please stay tuned for the final installment.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek