ASP.NET Tutorial/ASP.net Controls/AdRotator

Материал из .Net Framework эксперт
Версия от 12:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

AdRotator for displaying your ad (C#)

File: MainAds.xml
<?xml version="1.0" encoding="utf-8" ?> 
<Advertisements>
  <Ad>
    <ImageUrl>yourImage.gif</ImageUrl>
    <NavigateUrl>http://www.nfex.ru</NavigateUrl>
    <AlternateText>nfex.ru</AlternateText>
    <Impressions>1</Impressions>
  </Ad>
</Advertisements>

File: Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="AdRotatorTest" %>
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:AdRotator id="Ads" runat="server" 
     Target="_blank" AdvertisementFile="~/MainAds.xml" OnAdCreated="Ads_AdCreated" />
        <br />
        <br />
        <br />
        <asp:HyperLink ID="lnkBanner" runat="server">HyperLink</asp:HyperLink></div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class AdRotatorTest : System.Web.UI.Page
{
    protected void Ads_AdCreated(object sender, AdCreatedEventArgs e)
    {
        lnkBanner.NavigateUrl = e.NavigateUrl;
        lnkBanner.Text = "Click here for information about our sponsor: ";
        lnkBanner.Text += e.AlternateText;
    }
}


AdRotator randomly display different advertisements in a page

Important properties of AdRotator 
AdvertisementFile:    path to an XML file.
AlternateTextField:   alternate text. 
                      The default value is AlternateText.
DataMember:           bind to a particular data member in the data source.
DataSource:           set a data source.
DataSourceID:         bind to a data source declaratively.
ImageUrlField:        field for the image URL. 
                      The default value for this field is ImageUrl.
KeywordFilter:        filter advertisements by a single keyword.
NavigateUrlField:     advertisement link. 
 
Target:               open a new window when a user clicks the banner advertisement.


OnAdCreated event

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="AdTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
          <asp:AdRotator id="AdRotator1" 
                         runat="server" 
                         Target="_blank" 
                         AdvertisementFile="ads.xml" 
                         OnAdCreated="AdRotator1_AdCreated"></asp:AdRotator>
        <br />
        <br />
      <asp:HyperLink id="lnkBanner" runat="server">HyperLink</asp:HyperLink>
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class AdTest : System.Web.UI.Page
{
  protected void AdRotator1_AdCreated(object sender, AdCreatedEventArgs e)
  {
    lnkBanner.NavigateUrl = e.NavigateUrl;
    lnkBanner.Text = "Click here for information about our sponsor: ";
    lnkBanner.Text += e.AlternateText;
  }
}
File: ads.xml
<?xml version="1.0" encoding="utf-8" ?> 
<Advertisements>
  <Ad>
    <ImageUrl>a.gif</ImageUrl>
    <NavigateUrl>http://www.a.ru</NavigateUrl>
    <AlternateText>A</AlternateText>
    <Impressions>20</Impressions>
    <Keyword>books</Keyword>
  </Ad>
  <Ad>
    <ImageUrl>j.gif</ImageUrl>
    <NavigateUrl>http://www.j.ru</NavigateUrl>
    <AlternateText>J</AlternateText>
    <Impressions>20</Impressions>
    <Keyword>Java</Keyword>
  </Ad>
</Advertisements>


Storing Advertisements in a Database Table

<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
    <title>AdRotator Database</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:AdRotator
        id="AdRotator1"
        DataSourceID="srcAds"
        Runat="server" />
    <asp:SqlDataSource
        id="srcAds"
        ConnectionString="Server=.\SQLExpress;Integrated Security=True;
            AttachDbFileName=|DataDirectory|AdListDB.mdf;User Instance=True"
        SelectCommand="SELECT ImageUrl, Width, Height, NavigateUrl, AlternateText, Keyword, Impressions
            FROM AdList"
        Runat="server" />
    </div>
    </form>
</body>
</html>
AdList database table
Column Name            Data Type
Id                     Int (IDENTITY)
ImageUrl             Varchar(250)
Width                 Int
Height                 Int
NavigateUrl             Varchar(250)
AlternateText         NVarchar(100)
Keyword                 NVarchar(50)
Impressions             Int


Storing Advertisements in an XML File

<%@ Page Language="C#" %>
<!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" >
<body>
    <form id="form1" runat="server">
    <div class="content">
    <asp:AdRotator
        id="AdRotator1"
        AdvertisementFile="~/App_Data/Data.xml"
        KeywordFilter="banner"
        CssClass="banner"
        Runat="server" />
    <br />
    <div class="box">
        <asp:AdRotator
            id="AdRotator2"
            AdvertisementFile="~/App_Data/Data.xml"
            KeywordFilter="box"
            Runat="server" />
        <br /><br />
        <asp:AdRotator
            id="AdRotator3"
            AdvertisementFile="~/App_Data/Data.xml"
            KeywordFilter="box"
            Runat="server" />
    </div>

    <br />Here is the body text in the page.
    <br />Here is the body text in the page.
    <br />Here is the body text in the page.
    <br />Here is the body text in the page.
    <br class="clear" />
    </div>
    </form>
</body>
</html>
File: Data.xml
<Advertisements>
  <Ad>
    <ImageUrl>http://www.nfex.ru/style/logo.png</ImageUrl>
    <Width>300</Width>
    <Height>50</Height>
    <NavigateUrl>http://www.nfex.ru</NavigateUrl>
    <AlternateText>Banner Advertisement 1</AlternateText>
    <Impressions>50</Impressions>
    <Keyword>banner</Keyword>
  </Ad>
  <Ad>
    <ImageUrl>~/Ads/BannerAd2.gif</ImageUrl>
    <Width>300</Width>
    <Height>50</Height>
    <NavigateUrl>http://www.nfex.ru</NavigateUrl>
    <AlternateText>Banner Advertisement 2</AlternateText>
    <Impressions>25</Impressions>
    <Keyword>banner</Keyword>
  </Ad>
  <!-- Box Advertisements -->
  <Ad>
    <ImageUrl>http://www.nfex.ru/style/logo.png</ImageUrl>
    <Width>150</Width>
    <Height>150</Height>
    <NavigateUrl>http://www.nfex.ru</NavigateUrl>
    <AlternateText>Box Advertisement 1</AlternateText>
    <Impressions>50</Impressions>
    <Keyword>box</Keyword>
  </Ad>
  <Ad>
    <ImageUrl>~/Ads/BoxAd2.gif</ImageUrl>
    <Width>150</Width>
    <Height>150</Height>
    <NavigateUrl>http://www.nfex.ru</NavigateUrl>
    <AlternateText>Box Advertisement 2</AlternateText>
    <Impressions>50</Impressions>
    <Keyword>box</Keyword>
  </Ad>
</Advertisements>


Using the AdRotator control as a banner ad

<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>AdRotator Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:AdRotator ID="AdRotator1" Runat="server" 
         AdvertisementFile="MyAds.xml" />
        Lorem ipsum dolor sit 
        amet, consectetuer adipiscing elit. Duis vel justo. Aliquam 
        adipiscing. In mattis volutpat urna. Donec adipiscing, nisl eget 
        dictum egestas, felis nulla ornare ligula, ut bibendum pede augue 
        eu augue. Sed vel risus nec urna pharetra imperdiet. Aenean 
        semper. Sed ullamcorper auctor sapien. Suspendisse luctus. Ut ac 
        nibh. Nam lorem. Aliquam dictum aliquam purus.
    </form>
</body>
</html>
File: MyAds.xml
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements xmlns="http://schemas.microsoft.ru/AspNet/AdRotator-Schedule-File">
  <Ad>
    <ImageUrl>book1.gif</ImageUrl>
    <NavigateUrl>http://www.wrox.ru</NavigateUrl>
    <AlternateText>Visit Wrox Today!</AlternateText>
    <Impressions>50</Impressions>
    <Keyword>VB.NET</Keyword>
  </Ad>
  <Ad>
    <ImageUrl>book2.gif</ImageUrl>
    <NavigateUrl>http://www.wrox.ru</NavigateUrl>
    <AlternateText>Visit Wrox Today!</AlternateText>
    <Impressions>50</Impressions>
    <Keyword>XML</Keyword>
  </Ad>
</Advertisements>