• home
  • Email: trainings@synapseindia.careers

Blogs

back

Working With Adrotator Control in ASP.NET WebForms

Sep 30, 2015

In this article we will come to know about Adrotator Control which is one of the rich controls available in ASP.NET WebForms. Below is the ASP tag for Adrotator control:

<asp:AdRotator ID="AdRotator1" runat="server" Width="600px" Height="500" />

While working with this control, we have to maintain a XML file for mapping certain properties for this control ,,let us create an xml file with XMLFile.xml ,see the code below :

<?xml version="1.0" encoding="utf-8" ?>

<Advertisements>

<Ad>

<NavigateUrl>https://www.synapseindia.com</NavigateUrl>

<ImageUrl>~/Images/birthday.jpg</ImageUrl>

<AlternateText>This is Birthday Page</AlternateText>

<KeyWord>Birthday</KeyWord>

<Impressions>30</Impressions>

</Ad>

<Ad>

<NavigateUrl>http://www.hcltech.com/</NavigateUrl>

<ImageUrl>~/Images/life.bmp</ImageUrl>

<AlternateText>This is Life insurance Page</AlternateText>

<KeyWord>life</KeyWord>

<Impressions>40</Impressions>

</Ad>

<Ad>

<NavigateUrl>http://www.hcltech.com/</NavigateUrl>

<ImageUrl>~/Images/sunset.jpg</ImageUrl>

<AlternateText>This is Sunset Page</AlternateText>

<KeyWord>sunset</KeyWord>

<Impressions>55</Impressions>

</Ad>

<Ad>

<NavigateUrl>http://www.wipro.com/</NavigateUrl>

<ImageUrl>~/Images/twitter.jpg</ImageUrl>

<AlternateText>This is Teitter Page</AlternateText>

<KeyWord>twitter</KeyWord>

<Impressions>55</Impressions>

</Ad>

</Advertisements>

It contains the root tag <Advertisements> with various child tags name <Ad> which again have various more child tags name

<NavigateUrl></NavigateUrl>

<ImageUrl></ImageUrl>

<AlternateText></AlternateText>

<KeyWord></KeyWord>

<Impressions></Impressions>

All the above <tags> will contains the details which will be mapped to the respective properties of Adrotator controls , Hence Adrotator control will work with in the bond of these xml elements.

Now after creating the XML file , we have to create the default.aspx page which will contain the Adrotator1 control like below :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<h1 align="center">ADVERTISMENTS </h1>

<br />

<asp:AdRotator ID="AdRotator1" runat="server" Width="600px" Height="500"

AdvertisementFile="~/XMLFile.xml" onadcreated="AdRotator1_AdCreated" />

<br /><br />

<asp:Label ID="Label1" runat="server"></asp:Label><br />

<asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>

</div>

</form>

</body>

</html>

The above Adrotator1 control will automatically map with XML file by adding the name of the xml file in a property name AdvertisementFile like below :

AdvertisementFile="~/XMLFile.xml"

I have also added an event name onadcreated which will be invoked while adrotator1 will be created at server side . By this event , we can fetch the details of controls and can perform action on the basis of it. For which we have to write the logic on Default.aspx.cs file as given below :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void AdRotator1_AdCreated(object sender, AdCreatedEventArgs e)

{

Label1.Text = "Add name " + e.AlternateText;

HyperLink1.Text = e.AlternateText;

HyperLink1.NavigateUrl = e.NavigateUrl;

}

}

By considering the overall feedback , Adrotator is a good control for maintaining the advertisement on your website.

SynapseIndia is having good employees working on .NET Profile. SynapseIndia support anti-employee abuse policy. Along with this policy , SynapseIndia also support anti-campaign and policies.

SynapseIndia (CEO: Shamit Khemka)