Other ASP.NET Rich Controls


To finish off this chapter, we will look briefly at three other controls that are provided with ASP.NET. Rich controls create the entire HTML required for implementing a complex task in one simple operation. A typical example of this is the Calendar control, which can create a complete working calendar within a web page. All you do is add the control to your page and set a few properties. The three controls covered here are:

Control

Description

<ASP:AdRotator>

Displays an advertisement banner that changes on a predefined schedule.

<ASP:Calendar>

Displays a calendar showing single months and allows selection of a date.

<ASP:Xml>

Displays the content of an XML document or the result of an XSL or XSLT transformation.

Using the Rich Controls

The rich controls mentioned in this section are very useful when you need to perform the specific tasks for which they are designed. We don't have room to provide exhaustive coverage here, but by now you should be familiar with the way that server controls work, and you should have no problem getting to grips with these. You can use the demonstration application provided ( open the OtherControls section of the left-hand menu) to see the output they generate, and experiment with the various property settings.

The ASP:AdRotator Control

The AdRotator control has been part of ASP almost since the beginning, and is a popular way to display advertisement banners on a quasi-random pre-defined schedule. A new version is included as part of the default ASP.NET installation and it has a couple of extra features.

Probably the most useful feature is the ability to dynamically filter the list of banners that will be displayed, so that you can, for example, display banners for products or organizations that are relevant to a specific page or user . The filtering is carried out through the KeywordFilter property. This property is set in our demonstration application, as shown in Figure 6-26:

click to expand
Figure 6-26:

The code used for inserting the control into the page for this example is:

  <ASP:AdRotator id="MyControl" AdvertisementFile="adverts.xml"   runat="server" />  

Previous versions of the AdRotator control relied on a text file to specify the advertisements and the schedule for displaying each one. The new control uses an XML file to define the schedules. The file path is specified in the AdvertisementFile property, as shown in the source code for the page above (you can't change this property in the demonstration page).

The AdRotator Schedule File

An example of the format of the XML schedule file is shown in the following code:

  <Advertisements>   <Ad>   <ImageUrl>ads/asptoday.gif</ImageUrl>   <NavigateUrl>http://www.asptoday.com/</NavigateUrl>   <AlternateText>ASPToday</AlternateText>   <Impressions>20</Impressions>   <Keyword>Articles</Keyword>   </Ad>   <Ad>   <ImageUrl>ads/wrox.gif</ImageUrl>   <NavigateUrl>http://www.wrox.com/</NavigateUrl>   <AlternateText>Wrox Press</AlternateText>   <Impressions>20</Impressions>   <Keyword>Books</Keyword>   </Ad>   ... more <Ad> elements here ...   <Advertisements>  

The ImageUrl and NavigateUrl are self-explanatory. The AlternateText is used as the HTML alt attribute of the <img> element that the AdRotator control creates, and the Impressions value is used to control how often this banner will appear. The total for this element in all the <Ad> elements in the schedule file is calculated, and the ratio of impressions can then be calculated by the control and used to select the advertisement to display.

The Keyword element is used to allocate each advertisement to a category with that name . When you set the KeywordFilter property, only advertisements with the specified value for their Keyword element are included in the selection and display process.

The ASP:Calendar Control

By far the most complex server control in ASP.NET is the Calendar control. This creates a fully interactive one-month-at-a-time calendar as an HTML table. Figure 6-27 shows the default output from the Calendar control, and you can see that it really is a rich control in that it saves an incredible amount of effort on behalf of the developer:

click to expand
Figure 6-27:

The code used for inserting the control into the source of the page is:

  <ASP:Calendar id="MyControl" runat="server" />  

Notice that it automatically defaults to the current date (shown in the TodaysDate property in the next screesnhot) if you don't specify a date. If you scroll to the bottom of this page, you can see some of the properties that can be set to change the appearance. These don't include the various calendar-specific style properties that are available, such as DayHeaderStyle , DayStyle , MonthStyle , and so on (see Figure 6-28):

click to expand
Figure 6-28:

The Calendar control also exposes a couple of events that you can use to detect when the user interacts with the control. You can write an event handler for the SelectionChanged event, and obtain the date that the user selected by querying the SelectedDate property of the control within that event handler.

You can also create an event handler for the VisibleMonthChanged event. In this case, the second parameter of the event handler is a MonthChangedEventHandler object, which exposes two properties named NewDate and PreviousDate that contain the original (before the month was changed) and current dates (the date after the month was changed).

The ASP:Xml Control

The final control we're covering is the ASP:Xml server control. This can be used to display the content of an XML document, or to perform a server-side transformation on the document using a suitable XSL or XSLT stylesheet. Note that this control does not inherit from WebControl . It inherits directly from Control , and so doesn't have all the display-oriented properties that the other Web Form controls do.

Note

Don't be confused into thinking that this control creates an IE 5-style <xml> element “it doesn't. It is a server-side object that outputs XML or the result of an XSL or XSLT transformation to the client.

There are six properties available for the Xml control. You can specify the source document using one of the first three properties in the following table, and the stylesheet (if you're using one) in one of the next two properties shown in:

Property

Description

Document

A reference to an XmlDocument object that contains the XML document we want to display or transform. The XmlDocument object is covered in Chapter 11.

DocumentContent

A string containing the text of the XML document you want to display or transform.

DocumentSource

A string that contains the physical or virtual path to the XML document you want to display or transform.

Transform

A reference to an XslTransform object that contains the XSL or XSLT stylesheet to use for transforming the XML document before displaying it. The XslTransform object is covered in Chapter 11.

TransformSource

A string that contains the physical or virtual path to the XSL or XSLT stylesheet you want to use for the transformation.

TransformArgumentList

A reference to an XsltArgumentList object that contains the arguments to be passed to the stylesheet.

In Figure 6-29, the DocumentSource property in the demonstration page is set to books.xml . The control loads this document from disk and displays it in the page. Of course, the XML elements aren't visible, because the browser attempts to render the XML as though it were HTML and so only the text content of the elements is shown in the page. If you view the source in your browser, however, you'll see the XML that the control sends to the client. We have also included a hyperlink in the page so that you can open the XML document in a separate browser window and see it.

click to expand
Figure 6-29:

The code used for inserting the control into the source of the page is:

  <ASP:Xml id="MyControl" DocumentSource="xml/books.xml" runat="server" />  

If you compare the original disk file with the output of the control, you will also see that the control has removed insignificant whitespace from the result. In other words, it strips out all the carriage returns, spaces, and tab characters .

Using an XSL Stylesheet

We have provided three simple XSL stylesheets that you can use to transform the XML document. They transform the XML into HTML (because we are displaying it in the browser in our application). For example, Figure 6-30 shows how the reportview.xsl stylesheet generates a sales report from the data in the XML document:

click to expand
Figure 6-30:

The code used to apply the XSL stylesheet detects whether this is a postback and, if it is, sets the TransformSource property of the Xml control to the relative path to the XSL file:

  If IsPostBack() Then     If chkTransformSource.Checked _   And Left(selTransformSource.Value, 1) <> "{" Then   MyControl.TransformSource = "xml/" & selTransformSource.Value   End If     End If  



Professional ASP. NET 1.1
Professional ASP.NET MVC 1.0 (Wrox Programmer to Programmer)
ISBN: 0470384611
EAN: 2147483647
Year: 2006
Pages: 243

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net