Now That s Cool Rich Server Controls

   

Now That's Cool! Rich Server Controls

ASP.NET provides a few very rich Web server controls that create form and produce function that would take tons of code in any other environment. But in ASP.NET they can be achieved with a single tag. Chapter 1 touched on the Calendar Web server control; in this section you'll see that and the AdRotator Web server control, as well.

AdRotator

Anyone who has had to manage banner ads will love ASP.NET's AdRotator object. This Web server control enables you to deliver and maintain banner ad campaigns, filter what ads are delivered, and control how many times they are delivered, all through the use of a controlling XML file. Table 7.14 lists and describes its properties.

Table 7.14. AdRotator Object Properties

Property

Description

AdvertisementFile

Gets or sets the path to an XML file that contains advertisement information.

KeywordFilter

Gets or sets a category keyword to filter for specific types of advertisements in the XML advertisement file.

Target

Gets or sets the target where the page displayed by clicking the banner will be loaded, such as _blank, _parent, _self, _top, or the name of a frame in a frameset.

Now you're gonna get your first taste of XML, at least in this book. Don't be alarmed or overwhelmed with the concept of XML. If you take a look at the OurAds.xml file (in a few paragraphs) you will see it is quite simple to understand what's going on in this file. XML works a lot like HTML, using delimiters to define and contain information.

When you deal with HTML, if you want to put a Title into a page with HTML, you use a predefined tag such as <title>This is the title</title>. There is an opening delimiter and a closing delimiter for the predefined tag.

XML works in a similar manner, except the delimiters are not predefined. They are self-defining. Instead of me explaining, take a look at the example.

The XML file that defines the AdRotator information includes five necessary elements: ImageURL, NavigateURL, AlternateText, Keyword, and Impressions. The XML document identifies this through its tag delimiters.

OurAds.xml
<Advertisements>      <Ad>             <ImageUrl>images/nr_ad.gif</ImageUrl>              <NavigateUrl>http://www.newriders.com</NavigateUrl>               <AlternateText>New Riders</AlternateText>               <Keyword>Publishers</Keyword>               <Impressions>100</Impressions>      </Ad>         <Ad>                <ImageUrl>images/peter_ad.gif</ImageUrl>                <NavigateUrl>http://www.nexusmediagroup.com</NavigateUrl>                <AlternateText>It's Peter</AlternateText>                <Keyword>Authors</Keyword>                <Impressions>150</Impressions>         </Ad>  </Advertisements> 

Notice that there are tags that delimit where the advertisements are, where the ads begin and end, and where the attributes of each ad begin and end in this document. Not too hard to follow, right? Now look at the AdRotator in action. Even though there are only two ads in the XML file, I want to demonstrate the KeywordFilter property to limit the ads to be displayed to only those with a keyword of Authors. If there were many ads with this keyword, they would be randomly displayed.

Visual Basic .NET web_adrotator_vb.aspx
<%@ page language="vb" runat="server"%>  <html>  <head>  <title>Web Controls - Ad Rotator</title>  </head>  <body bgcolor="#FFFFFF" text="#000000">  <form runat="server">  <asp:AdRotator       KeywordFilter="Authors"      AdvertisementFile  = "OurAds.xml"      runat=server />  </form>  </body>  </html> 
C# web_adrotator_cs.aspx
<%@ page language="c#" runat="server"%>  <html>  <head>  <title>Web Controls - AdRotator</title>  </head>  <body bgcolor="#FFFFFF" text="#000000">  <form runat="server">  <asp:AdRotator       KeywordFilter="Authors"      AdvertisementFile  = "OurAds.xml"      runat=server />  </form>  </body>  </html> 

As you can see in Figure 7.10, the "Author that Blabbers" ad was delivered to the web page as expected. This control also has an event called AdCreate, which has its own set of event arguments. One of these arguments, called AdProperties, enables you to access any name-value pair within the XML document for the specific delivered ad.

Figure 7.10. The AdRotator web server control is a blessing for programmers that must create and manage banner ad campaigns.
graphics/07fig10.gif

So imagine that you had an additional entry in your ads called <message>Here is your message</message>. You could call a function when you create the ad through the AdCreate event and maybe set a label's text value to the value of <message> in your XML document.

Visual Basic .NET
Sub AdCreated_Event(sender As Object, e As AdCreatedEventArgs)            OurLabel.Text = e.AdProperties("Message")         End Sub 
C#
void AdCreated_Event(Object sender, AdCreatedEventArgs e){           OurLabel.Text = e.AdProperties["Message"];         } 

Calendar

Lordy, Lordy. The Calendar control sure does have a lot of properties all its own. Table 7.15 is one big table…but, as I describe briefly in Chapter 1, this control would take loads more time to code by hand than you need to learn and understand its properties.

Table 7.15. Calendar Object Properties

Property

Description

CellPadding

Gets or sets the amount of space between the contents of a cell and the cell's border.

CellSpacing

Gets or sets the amount of space between cells.

DayHeaderStyle

Gets the style properties for the section that displays the day of the week.

DayNameFormat

Gets or sets the name format of days of the week.

DayStyle

Gets the style properties for the days in the displayed month.

FirstDayOfWeek

Gets or sets the day of the week to display in the first day column of the Calendar control.

NextMonthText

Gets or sets the text displayed for the next month's navigation control.

NextPrevFormat

Gets or sets the format of the Next and Previous Month navigation elements in the title section of the Calendar control.

NextPrevStyle

Gets the style properties for the Next and Previous Month navigation elements.

OtherMonthDayStyle

Gets the style properties for the days on the Calendar control that are not in the displayed month.

PrevMonthText

Gets or sets the text displayed for the Previous Month navigation control.

SelectedDate

Gets or sets the selected date.

SelectedDates

Gets a collection of System.DateTime objects that represent the selected dates on the Calendar control.

SelectedDayStyle

Gets the style properties for the selected dates.

SelectionMode

Gets or sets the date selection mode on the Calendar control that specifies whether the user can select a single day, a week, or an entire month.

SelectMonthText

Gets or sets the text displayed for the month selection element in the selector column.

SelectorStyle

Gets the style properties for the week and month selector column.

SelectWeekText

Gets or sets the text displayed for the week selection element in the selector column.

ShowDayHeader

Gets or sets a value indicating whether the heading for the days of the week is displayed.

ShowGridLines

Gets or sets a value indicating whether the days on the Calendar control are separated with grid lines.

ShowNextPrevMonth

Gets or sets a value indicating whether the Calendar control displays the Next and Previous Month navigation elements in the title section.

ShowTitle

Gets or sets a value indicating whether the title section is displayed.

TitleFormat

Gets or sets the title format for the title section.

TitleStyle

Gets the style properties of the title heading for the Calendar control.

TodayDayStyle

Gets the style properties for today's date on the Calendar control.

TodaysDate

Gets or sets the value for today's date.

VisibleDate

Gets or sets the date that specifies the month to display on the Calendar control.

WeekendDayStyle

Gets the style properties for the weekend dates on the Calendar control.

Phewwww!! Yes, there are a lot of properties. No, it's not that complicated. If you read the properties in Table 7.14, they are all very understandable. If you need additional information, refer to the SDK, where you can get an exhaustive amount of information about every little nook and cranny of every property the Calendar Web server control possesses.

Note

I know that I've mentioned this before, but it's worth repeating. Whenever you need information about anything having to do with the .NET Framework, your best bet is to go to the SDK. It's as thorough a set of documents as you are going to find on the .NET Framework, and its navigation is intuitive and logical. Whenever you need to dig deeper into an object or control, you can always find links that will take you where you need to go. Want to find out about the Calendar object's properties? Navigate to the Calendar object and click to see its "Members." Want to know about the DayNameFormat property? Click its name, which is a link to a document about it. Want to see allowable values for this property? Click the DayNameFormat Value link. Dig deep. You can spend days digging through here.


Now look at an example that plays around with a bunch of the properties. You will see some of the properties hyphenated with what looks like properties from other classes. You can do this because these are actually instances of other objects and you can directly manipulate these instances' properties through this hyphenated system. This gives you awesome amounts of control over how Web server controls and, particularly in this example, calendars will look and act.

Visual Basic .NET web_calendar_vb.aspx
<%@ page language="vb" runat="server"%>  <script runat=server>  Sub SelectionChange(sender as Object, e as EventArgs)      OurLabel.Text = "The date you selected is " & OurCalendar.SelectedDate. graphics/ccc.gifToShortDateString()  end sub  </script>  <html>  <head>  <title>Web Control - Calendar</title>  </head>  <body bgcolor="#FFFFFF" text="#000000">  <form runat="server">      <asp:Calendar           CellPadding="2"          Font-Name="verdana"          Font-Size="12px"          FirstDayOfWeek="Monday"          TitleStyle-Font-Size="14px"          TitleStyle-BackColor="#CCCCCC"          NextMonthText=">>"          PrevMonthText="<<"          NextPrevStyle-Font-Bold="false"          DayHeaderStyle-BackColor="#000000"          DayHeaderStyle-ForeColor="#FFFFFF"          SelectedDayStyle-BackColor="#000000"          ShowGridLines="true"          SelectionMode="Day"          OnSelectionChanged="SelectionChange"          BorderColor="#000000"          runat="server" />  <br><br>  <asp:Label  runat="server" />  </form>  </body>  </html> 
C# web_calendar_cs.aspx
<%@ page language="cs" runat="server"%>  <script runat=server>  void SelectionChange(Object sender,EventArgs e){     OurLabel.Text = "The date you selected is " + OurCalendar.SelectedDate. graphics/ccc.gifToShortDateString();  }  </script>  <html>  <head>  <title>Web Control - Calendar</title>  </head>  <body bgcolor="#FFFFFF" text="#000000">  <form runat="server">      <asp:Calendar           CellPadding="2"          Font-Name="verdana"          Font-Size="12px"          FirstDayOfWeek="Monday"          TitleStyle-Font-Size="14px"          TitleStyle-BackColor="#CCCCCC"          NextMonthText=">>"          PrevMonthText="<<"          NextPrevStyle-Font-Bold="false"          DayHeaderStyle-BackColor="#000000"          DayHeaderStyle-ForeColor="#FFFFFF"          SelectedDayStyle-BackColor="#000000"          ShowGridLines="true"          SelectionMode="Day"          OnSelectionChanged="SelectionChange"          BorderColor="#000000"          runat="server" />  <br><br>  <asp:Label  runat="server" />  </form>  </body>  </html> 

In Figure 7.11 you can see that this code has formatted the tar out of the calendar, and this is just the tip of the iceberg. Experiment with the properties and you will quickly see that you can make this control do just about anything you'd like it to and make it look any way you'd like.

Figure 7.11. The Calendar control emits code that would take hours and hours to code by hand with the setting of some simple properties.
graphics/07fig11.gif


   
Top


ASP. NET for Web Designers
ASP.NET for Web Designers
ISBN: 073571262X
EAN: 2147483647
Year: 2005
Pages: 94
Authors: Peter Ladka

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