The document describes the ASP.NET AdRotator control which is used to display a sequence of ad images randomly by selecting from a predefined list stored in an XML file. The XML file contains <Advertisements> and <Ad> tags to define each ad along with elements like <ImageUrl>, <NavigateUrl>, <AlternateText>, <Keyword> and <Impressions>. The AdRotator control presents ads each time a page loads and navigates to a new URL when ads are clicked. It can fetch ad information from a database or XML file and display ads randomly with the help of a Timer control.
2. Definition and Usage
The AdRotator control is used to display a sequence of ad
images.
This control uses an XML file to store the ad information.
The XML file must begin and end with an
<Advertisements> tag. Inside the <Advertisements> tag
there may be several <Ad> tags which defines each ad.
The AdRotator Control presents ad images each time a user enters or refreshes a webpage.
When the ads are clicked, it will navigate to a new Web location. Each time the page is loaded
into the browser, an ad is randomly selected from a predefined list. Previously the predefined
list means an XML File, which contains the information about the ads to be displayed. But in
Asp.Net 2.0 we can maintain the list in any data source. In this article, we explained the
AdRotator control to fetch ads information from both database and XML file, and display those
ads in randomly with the help of Timer control. You can say it as an AJAX AdRotator Control.
3. The predefined elements inside the <Ad> tag are listed
below:
Element Description
<ImageUrl>
Optional. The path to the
image file
<NavigateUrl>
Optional. The URL to link
to if the user clicks the ad
<AlternateText>
Optional. An alternate text
for the image
<Keyword>
Optional. A category for
the ad
<Impressions>
Optional. The display rates
in percent of the hits
4. Properties
Property Description
.
N
E
T
AdvertisementFile
Specifies the path to the XML file
that contains ad information
1
.
0
AlternateTextField
Specifies a data field to be used
instead of the Alt text for an ad
2
.
0
ImageUrlField
Specifies a data field to be used
instead of the ImageURL
attribute for an ad
2
.
0
KeywordFilter
Specifies a filter to limit ads after
categories
1
.
0
NavigateUrlField
Specifies a data field to be used
instead of the NavigateUrl
attribute for an ad
2
.
0
runat
Specifies that the control is a
server control. Must be set to
"server"
1
.
0
Target Specifies where to open the URL
1
.
0
5. 5
Outline
FlagRotator.aspx
(3 of 3 )
AdRotator image
Fig. | Web Form that demonstrates the AdRotator
web control. (Part 3 of 3. )
a)
AlternateText
displayed in a tool tip
c)
b)
Add an AdRotator control named
countryRotator.
Add a XmlDataSource control, which
supplies the data to the AdRotator
control.
Figure (a) shows the first time the page is
requested, when the Swedish flag is
randomly chosen.
In the second request, as shown in Figure
(b), the French flag is displayed.
Figure (c) depicts the web page that loads
when you click the French flag.
6. AdRotator Example: FlagRotator
Connecting Data to an AdRotator Control
• An XmlDataSource references an XML file containing data that will
be used in an ASP.NET application.
• To build this example, we first add the XML file
AdRotatorInformation.xml to the project.
• Right click the App_Data folder in the Solution Explorer and select
Add Existing Item….
• Next, drag an AdRotator control from the Toolbox to the Web
Form.
7.
8. AdRotator Example: FlagRotator
• From the smart-tag menu, select <New Data Source…> from the
Choose Data Source drop-down list to start the Data Source
Configuration Wizard.
• Select XML File as the data-source type. Set the ID of the control
to adXmlDataSource.
• In the next dialog’s Data File section, click Browse… and, in the
Select XML File dialog, locate and select the XML file you added
to the App_Data folder.
9. Examining an XML File Containing Advertisement
Information
Any XML document used with an AdRotator control—must contain one
Advertisements root element.
Within that element can be as many Ad elements as you need. Each Ad element is
similar to the following:
<Ad>
<ImageUrl>Images/france.png</ImageUrl>
<NavigateUrl>https://www.cia.gov/library/publications/
the-world-factbook/geos/fr.html
</NavigateUrl>
<AlternateText>France Information</AlternateText>
<Impressions>1</Impressions>
</Ad>
10. Element ImageUrl specifies the path (location) of the
advertisement’s image.
Element NavigateUrl specifies the URL for the web
page that loads when a user clicks the advertisement.
The AlternateText element nested in each Ad element
contains text that displays in place of the image when the
browser cannot display the image.
The Impressions element specifies how often a
particular image appears, relative to the other images. In
our example, the advertisements display with equal
probability, because the value of each Impressions
element is set to 1.
14. Data Controls
ASP.NET comes with two sets of data-aware controls:
the data-bound and the data source controls.
Data-bound controls
These are used to display and edit data. The
GridView, DataList, ListView, and
Repeater are all able to display multiple records
at the same time, whereas the DetailsView and
the FormView are designed to show a single record
at a time. The DataPager is used to provide
paging capabilities to the ListView controls.
15. Definition and Usage
Repeater – It gives you the greatest flexibility in terms
of the HTML that you output to the browser as the
control by itself does not add any HTML to the page
output. The control has no built-in capabilities for the
paging, sorting and modification of data. It is often used
when you need precise control over the markup that is
generated by the control. As such, it’s often used for
HTML ordered or unordered lists (<ol> and <ul>), etc.
16. Repeater has 5 inline template to format it:
1. <HeaderTemplate>
2. <FooterTemplate>
3. <ItemTemplate>
4. <AlternatingItemTemplate>
5. <SeperatorTemplate>
6. <AlternatingItemTemplate>
HeaderTemplate: This template is used for elements that you want to render once before your
ItemTemplate section.
FooterTemplate: - This template is used for elements that you want to render once after your
ItemTemplate section.
ItemTemplate: This template is used for elements that are rendered once per row of data. It is used to
display records
AlternatingItemTemplate: This template is used for elements that are rendered every second row of
data. This allows you to alternate background colors. It works on even number of records only.
SeperatorTemplate: It is used for elements to render between each row, such as line breaks.
17. How to use a Repeater Control?
Create a new website with name RepeaterControl
Double click the web.config file and write following code in it:
21. Code behind file code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
cmd.Connection = con;
cmd.CommandText = "select * from student";
con.Open();
RepeatInformation.DataSource = cmd.ExecuteReader();
RepeatInformation.DataBind();
con.Close();
}
}