【IT168 技术文档】
一、 AdRotator控件的三种数据源模式
AdRotator以三种不同的模式填充它的图像和导航属性,我把它们分别称为“数据库模式”,“XML模式”和“编程模式”。
(一) 数据库模式
在数据库模式下,一个AdRotator链接到一个数据源。该数据源表格至少需要三个数据列:一个相应于图像的URL,一个相应于该图像被点击时要导航到的URL,一个相应于要在该图像的ALT标签中被显示的字符串。下列是数据源表格中的可用字段,其中后面几个是可选的:
| 列名称 | 数据类型 | 说明 |
| ID | int | 这是主键。此列可给定任何名称。 |
| ImageUrl | nvarchar(length) | 要作为广告显示的图像的相对或绝对URL。 |
| NavigateUrl | nvarchar(length) | 广告的目标URL。如果没有提供值,则广告不是一个超链接。 |
| AlternateText | nvarchar(length) | 找不到图像时显示的文本。有些浏览器中,该文本还会作为工具提示显示出来。替换文字也用于辅助功能,以便无法看到图形的用户可以听到大声读出的说明。 |
| Keyword | nvarchar(length) | 可作为页筛选依据的广告类别,可选。 |
| Impressions | int(4) | 一个指示广告的可能显示频率的数字。数字越大,显示该广告的频率越高。在XML文件中,所有Impressions值的总和不能超过2,048,000,000-1。可选。 |
| Width | int(4) | 图像的宽度(以像素为单位),可选。 |
| Height | int(4) | 图像的宽度(以像素为单位),可选。 |
<asp:SqlDataSource ID="ProductPhotos"
runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
SelectCommand="SELECT Production.ProductPhoto.ProductPhotoID AS ID,
'./ProductThumbNail.ashx?ID=' + LTRIM(STR(Production.ProductPhoto.ProductPhotoID)) AS ImageUrl,
'./ProductPhoto.ashx?ID=' + LTRIM(STR(Production.ProductPhoto.ProductPhotoID)) AS NavigateUrl,
Production.Product.Name AS AlternateText
FROM Production.ProductPhoto
INNER JOIN Production.ProductProductPhoto
ON Production.ProductPhoto.ProductPhotoID = Production.ProductProductPhoto.ProductPhotoID
INNER JOIN Production.Product
ON Production.ProductProductPhoto.ProductID = Production.Product.ProductID">
</asp:SqlDataSource>
<asp:AdRotator ID="AdRotator1" runat="server" DataSourceID="ProductPhotos" />
<Ad>
<ImageUrl>~/ProductThumbNail.ashx?ID=70</ImageUrl>
<NavigateUrl>~/ProductPhoto.ashx?ID=70</NavigateUrl>
<AlternateText>Product Number 70</AlternateText>
<Impressions>100</Impressions>
</Ad>
<asp:AdRotator ID="AdRotator2" runat="server" AdvertisementFile="~/Advertisments.xml" />
<asp:AdRotator ID="AdRotator3" runat="server" OnAdCreated="AdRotator3_AdCreated"/>
protected void AdRotator3_AdCreated(object sender, AdCreatedEventArgs e) ...{
int ProductID = 1;
e.ImageUrl = "~/ProductThumbNail.ashx?ID=" + ProductID.ToString();
e.NavigateUrl = "~/ProductPhoto.ashx?ID=" + ProductID.ToString();
e.AlternateText = "以编程方式从产品ID生成文本 " + ProductID.ToString();
}