CODEDIGEST
Home Articles CodeDigest Tutorials FAQs
Skip Navigation LinksHome » Article » ASP.Net Article » Using ADRotator Control in ASP.Net-Part 2   You are not logged in.
Search
 

Technologies
 

Sponsors
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Using ADRotator Control in ASP.Net - Part 2

By Satheesh Babu
Posted On Jul 17,2010
Article Rating: (Login)
Be first to rate
this article.
No of Comments: 0
Print this article.
Category: ASP.Net

Subscribe to our feed!

Using AdRotator Control in ASP.Net - Part 2

Tracking Ad clicks

 

In Part 1 of this article series, we have seen some of the basic usages of AdRotator control in an ASP.Net page. In Part 2, we will see how to track an advertisement performance by adding a hit counter for each advertisement. To understand this, we will use database instead of XML file to render the ads with AdRotator control.

 

Tracking Advertisement Using AdRotator control

To track the advertisement in terms of number of clicks or hits, we need to pass the user to the destination page (ad’s landing page) through a proxy webpage or an HttpHandler. This proxy can then update your click into the database and will redirect the user to target page.

Below is the database design which we can use.

 

Building a Simple Ad management system in ASP.Net using AdRotator control

 

Drag an AdRotator control and a SqlDataSource control from your visual studio toolbar. Bind the AdRotator control through DataSourceID property after configuring the datasource control.

 

As i said before, we need to create a proxy which can record the clicks and redirects the user to target page. We will use HttpHandler to do this. Include a new HttpHandler to your solution through “Add new Item..” dialog box, name it as AdHandler.ashx.

 

Refer the below code,

 

public class AdHandler : IHttpHandler

    {

        public void ProcessRequest(HttpContext context)

        {

            string url = context.Request.QueryString["AdUrl"];

            try

            {

                AddHit(url);

            }

            catch

            {

            }

            finally

            {

                context.Response.Redirect(url);

            }

        }

        public int AddHit(string url)

        {

            SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString);

            con.Open();

            SqlCommand com = new SqlCommand("AddHit", con);

            com.CommandType = System.Data.CommandType.StoredProcedure;

            com.Parameters.Add(new SqlParameter("@url",url));

            int res = com.ExecuteNonQuery();

            con.Close();

            return res;

        }

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

SP

ALTER PROCEDURE dbo.AddHit

       (

       @url varchar(200)

       )     

AS

       DECLARE @ADID INT

      

       SELECT  @ADID = AdID from ADs where NavigateUrl = @url

      

       IF exists (Select ADID FROM AdHits where ADID = @ADID)

              UPDATE ADHits SET Hits = Hits+1 where ADID = @ADID

       ELSE  

       INSERT INTO ADHits values (@ADID,1)

RETURN

 

This HttpHanlder will accept the ad’s landing page (or NavigateUrl) as a querystring parameter to record the click into our database. Once it is recorded, the user will be redirected to the target ad’s page.

Now, we need to make the AdRotator control to use this proxy handler to redirect the user instead of passing them directly.

 

Well, this can be done by 2 ways.

1.      By AdCreated event

The AdCreated event will expose the advertisements NavigateUrl, ImageUrl, AlternateText properties which can be changed. You can use this event to pass the user through the proxy handler. Refer below,

ASPX

<asp:AdRotator ID="AdRotator1" runat="server" DataSourceID="SqlDataSource1"

            onadcreated="AdRotator1_AdCreated" />

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"

        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

        SelectCommand="SELECT * FROM [Ads]"></asp:SqlDataSource>

 

CodeBehind

protected void AdRotator1_AdCreated(object sender, AdCreatedEventArgs e)

        {

            e.NavigateUrl = "AdHandler.ashx?AdUrl=" + e.NavigateUrl;

        }

 

2.      By Ad XML or Database table

When configuring the NavigateUrl property of the ad, you can use the proxy handler. Something like below,

<Ad>

    <ImageUrl>~/img/AccordionPanel.jpg</ImageUrl>

    <NavigateUrl>      AdHandler.ashx?AdUrl=http://www.codedigest.com/Articles/jQuery/335_Creating_Accordion
_Panel_using_jQuery_in_ASPNet_%e2%80%93_Part_1.aspx

    </NavigateUrl>

    <AlternateText>

      Creating Accordion Panel using jQuery in ASP.Net

    </AlternateText>   

  </Ad>

 

We can now get the report of hits/clicks of the ads from the ADHits table.

 

ASP.Net Hosting

Recent Articles

Change Advertisements without page refresh in AdRotator control

One disadvantage of AdRotator control is, it will change the ad only if user refreshes the page or for the next page refresh. It will be really good if we can change the ads at regular intervals even if the user did not refresh the page. This can be done by using the ASP.Net AJAX update panel control and Timer control.

Read below code snippet which will help us to implement the same.

Change Advertisement in AdRotator without Page Refresh at Regular Interval

 

Downloads

Download Source 

Conclusion

Creating an advertisement management system ourselves is complicated and time consuming task. Most of the time, we can use the AdRotator control in ASP.Net to build a basic ad management system that can satisfy our needs.

Download the source attached with this article to see it in action.

Happy Coding!!

 

Similar Articles
  • You can contribute to CodeDigest.Com:
    Article Feedback
    Title  
    Submitted By  
    Comment  
    Enter the verification number
     
    Comments