AdRotator and AdRepeater Control in Asp.Net for Msc CS

Post on 31-Jul-2015

97 views 1 download

Tags:

transcript

ASP.NET AdRotator Control

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.

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

Properties

Property Description .NET

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

ImageUrlFieldSpecifies 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

NavigateUrlFieldSpecifies a data field to be used instead of the NavigateUrl attribute for an ad

2.0

runatSpecifies that the control is a server control. Must be set to "server"

1.0

Target Specifies where to open the URL 1.0

5

Outline

FlagRotator.aspx

(3 of 3 )

Fig. | Web Form that demonstrates the AdRotatorweb control. (Part 3 of 3. )

a)

AlternateTextdisplayed in a tool

tip

AdRotator image

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.

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.

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.

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>

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.

ASP.NET Repeater Control

Data ControlsASP.NET comes with two sets of data-aware controls:

the data-bound and the data source controls.Data-bound controlsThese 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.

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.

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.

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:

<connectionStrings> <add name="constr" connectionString="initial catalog=puran; data source=MCN002; integrated security=sspi"/> </connectionStrings>

Default.aspx file code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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"> <link rel="Stylesheet" type="text/css" href="StyleSheet.css" /> <title>Repeater Controls in ASP.NET</title></head><body> <form id="form1" runat="server"> <div> <asp:Repeater ID="RepeatInformation" runat="server"> <HeaderTemplate> <table class="tblcolor">

<tr> <b> <td> Roll No </td> <td> Student Name </td> <td> Total Fees </td> </b> </tr> </HeaderTemplate> <ItemTemplate> <tr class="tblrowcolor"> <td> <%#DataBinder.Eval(Container,"DataItem.RollNo")%> </td> <td> <%#DataBinder.Eval(Container,"DataItem.Name")%> </td> <td> <%#DataBinder.Eval(Container,"DataItem.Fees")%> </td> </tr> </ItemTemplate> <SeparatorTemplate>

<hr /> </td> </tr> </SeparatorTemplate> <AlternatingItemTemplate> <tr> <td> <%#DataBinder.Eval(Container,"DataItem.RollNo")%> </td> <td> <%#DataBinder.Eval(Container,"DataItem.Name")%> </td> <td> <%#DataBinder.Eval(Container,"DataItem.Fees")%> </td> </tr> </AlternatingItemTemplate> <SeparatorTemplate> <tr> <td> <hr /> </td> <td> <hr /> </td> <td> <hr /> </td> </tr> </SeparatorTemplate> <FooterTemplate> <tr> <td>

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(); }}

Thanvi