ASP.NET 移动控件之广告控件和日历控件

可以在 Visual Studio 中或使用任何文本编辑器来创建 ASP.NET 移动网页。许多控件都与 ASP.NET Web 服务器控件相似。例如, System.Web.UI.MobileControls.LabelSystem.Web.UI.MobileControls.TextBox 控件模仿 ASP.NET System.Web.UI.WebControls.LabelSystem.Web.UI.WebControls.TextBox 控件的行为。

在 ASP.NET 中可使用下列移动控件:

AdRotator

Calendar

Command

CompareValidator

CustomValidator

Form

Image

Label

Link

List

MobilePage

ObjectList

Panel

PhoneCall

RangeValidator

RegularExpressionValidator

RequiredFieldValidator

SelectionList

StyleSheet

TextBox

TextView

ValidationSummary


AdRotator 类

提供用于在移动页上显示随机选择的广告的服务器控件。

命名空间:System.Web.UI.MobileControls
程序集:System.Web.Mobile(在 system.web.mobile.dll 中)

public class AdRotator : MobileControl
 备注

AdRotator 类使用和 Web 窗体 AdRotator 类相同的配置文件。System.Web.UI.MobileControls.AdRotator 移动控件公开了许多与 System.Web.UI.WebControls.AdRotator 控件相同的属性和事件,另外它还添加了移动功能。每当刷新页时,显示的广告就会相应更改。

AdvertisementFile 属性指定包含广告信息的 XML 文件的位置。此文件必须驻留在应用程序域中。出于安全目的,XML 文件应该位于 App_Data 文件夹中,该文件夹配置为不允许用户直接访问 XML 文件。如果 AdvertisementFile 属性为空,AdRotator 控件生成一个分行标记作为占位符。当您不想显示广告时这很有用。

下面的代码示例由两部分组成:.aspx 文件和 XML 文件中的 ASP.NET 移动 Web 窗体页。.aspx 文件使用名为 ads.xml 的文件根据 KeywordFilter 属性在各种广告间切换。如果您提供示例图像,则将显示这些图像;否则,AdRotator 控件将显示广告的 AlternateText 属性的值。用户刷新移动 Web 窗体页后,该页面根据 KeywordFilter 显示下一个随机选择的广告。

该示例还演示如何在用户的浏览器需要 WML 标记的情况下使用 <Choice> 元素(《.NET Framework 开发人员指南》) 元素重写属性 (Property),以及如何使用 ImageKeyNavigateUrlKey 属性 (Property) 分别将图像的 srchref 属性 (Attribute) 映射到 XML 文件中的数据。

尽管该示例使用函数确定浏览器是否需要 WML (isWML11),但是也可以使用 Web.config 文件来定义 DeviceSpecific 元素,.NET Framework 将自动使用该元素进行判断:


<%@ Page Language="C#"
    Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
    Namespace="System.Web.UI.MobileControls" %>
<%@ Import Namespace="System.Web.Mobile" %>

<script runat="server">
    private void AdCreated_Event(Object sender, AdCreatedEventArgs e)
    {
       Label2.Text = "Clicking the AdRotator control takes you to " +
           e.NavigateUrl;
    }

    // Determine whether the current browser is a WML brower
    public bool isWML11(MobileCapabilities caps, string optValue)
    {
        // Determine if the browser is not a Web crawler and
        // requires WML markup
        if (!caps.Crawler && caps.PreferredRenderingType ==
            MobileCapabilities.PreferredRenderingTypeWml11)
            return true;
        else
            return false;
    }
</script>

<html  >
<body>
    <mobile:form id="form1" runat="server">
        <!-- The AdRotator control -->
        <mobile:AdRotator id="AdControl" runat="server"
            ImageKey="MobileImgSrc" NavigateUrlKey="TargetUrl"
            AdvertisementFile="App_Data/ads.xml" Alignment="Left"
            KeywordFilter="Developer" OnAdCreated="AdCreated_Event">
            <DeviceSpecific>
                <Choice Filter="isWML11" NavigateUrlKey="WmlTargetUrl"
                    ImageKey= "WmlImageSrc" />
            </DeviceSpecific>
        </mobile:AdRotator>
       
        <!-- The instructions label -->
        <mobile:Label id="Label1" runat="server"
            Text="Refresh the page to change the advertisement" />

        <!-- The URL info label -->
        <mobile:Label id="Label2" runat="server" />
    </mobile:form>
</body>
</html>


示例 ads.xml 文件(必须位于“App_Data”文件夹中):

<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
  <Ad>
    <WebImgSrc>imgA1.gif</WebImgSrc>
    <MobileImgSrc>imgA2.gif</MobileImgSrc>
    <WmlImgSrc>imgA3.gif</WmlImgSrc>
    <TargetUrl>http://msdn.microsoft.com/</TargetUrl>
    <WmlTargetUrl>http://OurServer/MS-MSDN.wml</WmlTargetUrl>
    <AlternateText>MSDN</AlternateText>
    <Keyword>Developer</Keyword>
    <Impressions>80</Impressions>
  </Ad>
  <Ad>
    <WebImgSrc>imgB1.gif</WebImgSrc>
    <MobileImgSrc>imgB2.gif</MobileImgSrc>
    <WmlImgSrc>imgB3.gif</WmlImgSrc>
    <TargetUrl>http://www.microsoft.com/</TargetUrl>
    <WmlTargetUrl>http://OurServer/MS-Home.wml</WmlTargetUrl>
    <AlternateText>Microsoft</AlternateText>
    <Keyword>Customer</Keyword>
    <Impressions>90</Impressions>
  </Ad>
  <Ad>
    <WebImgSrc>imgC1.gif</WebImgSrc>
    <MobileImgSrc>imgC2.gif</MobileImgSrc>
    <WmlImgSrc>imgC3.gif</WmlImgSrc>
    <TargetUrl>http://www.microsoft.com/net/</TargetUrl>
    <WmlTargetUrl>http://OurServer/MS-Net.wml</WmlTargetUrl>
    <AlternateText>.NET</AlternateText>
    <Keyword>Developer</Keyword>
    <Impressions>80</Impressions>
  </Ad>
</Advertisements>

Calendar 类

提供用于显示日历的控件功能。

命名空间:System.Web.UI.MobileControls
程序集:System.Web.Mobile(在 system.web.mobile.dll 中)

C#
public class Calendar : MobileControl, IPostBackEventHandler
 备注

日历按天、周或月显示。移动设备上是否呈现整月取决于该设备的功能。通常,Calendar 控件允许选择日期。

移动控件 Calendar 包装 Web 窗体控件 Calendar。尽管移动控件 Calendar 与相应的基础控件的有些属性、方法和事件类似,但该控件不公开特定于 HTML 呈现的其他属性。若要修改这些属性,可以通过 WebCalendar 属性访问基础控件,也可以直接进行修改。

下面的代码示例演示页加载代码块中的 SelectionMode 属性如何使用户可以选择天、周或月时间块。此示例设置 Calendar 类的 BorderStyleBackColor 属性来区分用户选择。


<%@ Page Language="C#"
    Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Register TagPrefix="mobile"
    Namespace="System.Web.UI.MobileControls"
    Assembly="System.Web.Mobile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        // Display the day header
        Calendar1.ShowDayHeader = true;
       
        // This allows the user to select a week or a month at a time.
        Calendar1.SelectionMode =
           CalendarSelectionMode.DayWeekMonth;
        // Set the BorderStyle and BorderColor properties.
        Calendar1.WebCalendar.DayStyle.BorderStyle =
           BorderStyle.Solid;
        Calendar1.WebCalendar.DayStyle.BorderColor = Color.Cyan;

        Calendar1.CalendarEntryText = "Your birthdate";
       
        Calendar1.VisibleDate = DateTime.Parse("7/1/" +
            DateTime.Now.Year.ToString());
    }

    protected void ShowChanges(Object sender, EventArgs e)
    {
        TextView1.Text = "The date you selected is " +
           Calendar1.SelectedDate.ToShortDateString();
       
        // Distinguish the selected block using colors.
        Calendar1.WebCalendar.SelectedDayStyle.BackColor =
           Color.LightGreen;
        Calendar1.WebCalendar.SelectedDayStyle.BorderColor =
           Color.Gray;
        Calendar1.WebCalendar.DayStyle.BorderColor = Color.Blue;
    }

    protected void Command1_Click(object sender, EventArgs e)
    {
        int currentDay = Calendar1.VisibleDate.Day;
        int currentMonth = Calendar1.VisibleDate.Month;
        int currentYear = Calendar1.VisibleDate.Year;
        Calendar1.SelectedDates.Clear();

        // Add all Wednesdays to the collection.
        for (int i = 1; i <= System.DateTime.DaysInMonth(currentYear,
               currentMonth); i++)
        {
            DateTime targetDate = new DateTime(currentYear, currentMonth, i);
            if (targetDate.DayOfWeek == DayOfWeek.Wednesday)
                Calendar1.SelectedDates.Add(targetDate);
        }
        TextView1.Text = "Selection Count ="
           + Calendar1.SelectedDates.Count.ToString();
    }
</script>

<html  >
<body>
    <mobile:form id="form1" runat="server">
        <mobile:Calendar id="Calendar1" runat="server"
            OnSelectionChanged="ShowChanges" />
        <mobile:TextView runat="server" id="TextView1" />
        <mobile:Command ID="Command1" OnClick="Command1_Click"
            Runat="server">Select Wednesdays</mobile:Command>
    </mobile:form>
</body>
</html>


posted @ 2009-09-10 23:57  minmin8110  阅读(241)  评论(0)    收藏  举报