Special Edition Using Visual Basic.NET

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } }
Team-Fly    

Special Edition Using Microsoft® Visual Basic® .NET

By Brian Siler, Jeff Spotts

Table of Contents
Chapter 19.  Web Controls

In this chapter we have covered several different categories of Web controls. Although the controls in this final section provide some useful and interesting features, they don't fit neatly into any one category. Therefore, we've lumped them together as special-purpose controls.

Using the Calendar Control

One very useful ASP Server control is the Calendar control. Like its Windows counterpart, the Web Calendar control provides an onscreen calendar, making it easier for the user to select a date. For example, you could include both a text box date field and a Calendar control on your form to allow the user the flexibility of typing the date quickly or browsing with the calendar. Some examples of the Calendar control are shown in Figure 19.16.

Figure 19.16. The Calendar control allows you to set individual styles and fonts for many of its different elements.

Making Your Calendar Stylish

As you might imagine, properties are available to customize the appearance of the Calendar control. For example, the ShowGridLines property determines whether lines are displayed between the days of your calendar. Many of the properties you use to tailor the calendar's appearance are style properties, which means they have multiple parts; you set the font, color, alignment, and other aspects individually to create a desired style. You can also link the style to a predefined CSS class, as described earlier. The Calendar control contains the following style properties:

  • DayHeaderStyle Sets the headings for the days of the week labels. You can also choose what format to display these in (that is, M, Mon, Mo, Monday) by setting the DayNameFormat property.

  • DayStyle Sets the style for the days in the currently displayed month.

  • NextPrevStyle Sets the style for the navigation buttons that allow the user to select the next or previous month. Setting the ShowNextPrevMonth property to False hides these navigation buttons, although the user can still move from month to month by clicking a day in the next or previous month.

  • OtherMonthDayStyle Sets the style for days from adjacent months. To hide these days, set the ForeColor property to match the background color.

  • SelectedDayStyle Controls the style for the date or day selected by the user.

  • SelectorStyle To accommodate the selection of multiple days, a selector column can be added to the calendar; control the style by setting the SelectorStyle property.

  • TitleStyle Allows you to customize the style for the month name at the top of the calendar. You can also hide the calendar title by setting the ShowTitle property to False, or you can customize the text by setting the TitleFormat property.

  • TodayDayStyle You can apply special formatting to the current date by setting this style property.

  • WeekendDayStyle If you want to use the calendar to draw attention to workdays or weekends, you can use this property to make the weekend style different. You can also set the FirstDayofWeek property to move a particular day to the leftmost column of the calendar.

The previous settings provide a lot of control over the way a calendar looks. If you want it to look better than it does by default, but do not want to have to set a bunch of individual properties, you can use one of the Autoformat settings. Just display the Properties window for a calendar and click Autoformat to select a Classic, Simple, or Professional calendar.

Note

Many of the navigation and selection elements of the calendar appear as a greater-than symbol (>) or a less-than symbol (<). Because HTML uses these symbols to delimit tags, the special characters &gt and &lt are used in HTML to represent greater than and less than, respectively. You can customize these navigation elements by entering your own custom text.

Selecting a Date

Use the SelectedDate property of a Calendar control to set or retrieve the currently selected date. This property can be set using a variable of type DateTime or by using a literal date. The following code sample illustrates setting and retrieving this property:

'Retrieves the Selected Date Dim dtSelDate AS DateTime dtSelDate = calmain.SelectedDate 'Sets the Selected Date calMain.SelectedDate = #8/16/1977.htm# 'Displays August 1977 month calMain.TodaysDate = #8/1/1977.htm#

Notice the final line of the sample code also sets the TodaysDate property. This is one of the ways in which the Web Calendar control behaves differently from its Windows counterpart; it does not automatically update the calendar to display the month specified by the SelectedDate property.

Note

When you select a date in the Calendar control, the SelectionChange event is fired on the server. You can use this event to update a text box or other field associated with the calendar.

Working with Multiple Dates

Another feature of the Web Calendar control is the selection of multiple days. Although this functionality is more limited than in the Windows version, the user can select a week or an entire month by clicking special selectors. However, the user cannot select arbitrary days in different weeks, or multiple weeks, using the user interface. To determine the way the Calendar control handles multiple date selection, set the SelectionMode property to one of the following values:

  • None No selection is allowed, the calendar dates do not contain hyperlinks, and the dates will not raise events.

  • Day Allows the user to select a single date. This is the default setting.

  • DayWeek Adds a week selector links for selecting a week. Selecting individual days is still possible by clicking them.

  • DayWeekMonth Adds hyperlinks for selecting weeks and the entire month. Selecting individual days is still possible by clicking them.

Note

Although the selection capability is somewhat limited through the Calendar interface, you can select any group of dates by setting the SelectedDates property.

When you are working with multiple dates, you use the SelectedDates property instead of the SelectedDate property to determine or set the selected days of the calendar:

Dim DesiredDates As SelectedDatesCollection Dim i As Integer DesiredDates = calMain.SelectedDates For i = 0 To DesiredDates.Count - 1 Call MarkScheduleBusy(DesiredDates(i)) Next

The SelectedDates property is a collection of type SelectedDatesCollection that can contain zero or more DateTime values.

Selling Out with the AdRotator

Just a few years ago, if someone had suggested that Visual Basic programmers needed an easy way to display advertisements in their programs, I would have laughed. However, the ubiquitous banner ad has become such a part of Web culture that Microsoft has deemed it necessary to incorporate this task into an ASP.NET control: the AdRotator. When placed on a Web form, the AdRotator control will automatically pick and display advertisements for you. Each advertisement is defined by the following properties:

  • ImageURL The URL of the advertisement image

  • NavigateURL The address of the Web page that opens when the user clicks the advertisement

  • Impression An integer which controls the likelihood of an ad being displayed

You can define an ad dynamically in code, but it is even easier to create an XML file containing the ad specifications. The following XML file defines an advertisement:

<Advertisements> <Ad> <ImageUrl>/images/vbbook.gif</ImageUrl> <NavigateUrl>http://www.vbinsider.com</NavigateUrl> <Impressions>50</Impressions> </Ad> </Advertisements>

To make the sample ad work with an AdRotator control, all you need to do is create a file containing the XML code in the virtual directory for the project and set the AdvertisementFile property of the AdRotator control to the filename.

Note

To learn more about XML and its role in the .NET world, see the help topic "XML in Visual Studio."

Note

Because the AdRotator control shrinks or expands the ad images to meet its dimensions, be sure to use a consistent image size for your ad images. A common advertisement size used on the Web is 468 pixels wide by 60 pixels high.

When you have defined multiple advertisements, the Impressions property determines how the AdRotator picks the ad to display. A higher number can be used to make a particular ad appear more often. If this method of picking ads is too simplistic, the AdRotator also offers the ability to include your own ad-selection logic. Instead of setting the AdvertisementFile property, you can set the ImageURL and other ad properties dynamically by adding code to the AdCreated event.

Using the CrystalReportViewer Control

For better or worse, Crystal Reports remains married to VB as the default reporting tool. The CrystalReportViewer control provides an easy way to display a Crystal Report on a Web form. In Chapter 23, "Creating and Using Reports," you learn how to design a Crystal Report. For the purposes of this chapter, we will assume you have already created a report and concentrate on how to display it in your application. Figure 19.17 shows the CrystalReportViewer in action.

Figure 19.17. Although severely crip-pled by lack of printing capability, the Crystal ReportViewer control makes it easy for Web application users to browse reports online.

For more on designing Crystal Reports, p. 637

As you can see from Figure 19.17, the CrystalReportViewer control has a toolbar. One of the main purposes of this toolbar is navigation; as the user clicks buttons to display the next or previous page, the CrystalReportViewer control automatically handles drawing the appropriate page.

Controlling Report Viewer Dimensions

When you draw a CrystalReportViewer control on a Web form, it appears as a simple gray square; the actual report viewer control is not rendered until you run the application. The default behavior for the CrystalReportViewer control is to take up the entire page. However, if you have other controls on your Web form, you will need to set the dimensions of the report viewer so it does not overlap them. To do this, you first need to set the BestFitPage property to False. Then, size the control to the desired dimensions. You can also display a border around the report viewer by setting the BorderStyle, BorderWidth, and BorderColor properties.

Choosing the Report File

When you create a Crystal Report, the layout and data source information are stored in a file with an .rpt extension. To let the CrystalReportViewer control know which report file to use, you should set the ReportSource property to the name of the file in the Page_Init event, as in the following example:

Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() MyCRViewer.ReportSource = "c:\inetpub\wwwroot\_ myapp\MonthlySales.rpt" Dim RegionParameter As CrystalDecisions.Shared.ParameterField Dim RegionValue As New CrystalDecisions.Shared._ ParameterDiscreteValue() RegionParameter = MyCRViewer.ParameterFieldInfo(0) RegionValue.Value = "EAST" RegionParameter.CurrentValues.Add(RegionValue) End Sub

Note that in the previous code sample we used the absolute path to the report file. At the time of this writing, a URL-based path or relative path would not work. Another common task when setting up the CrystalReportViewer control is assigning values to parameters required by the report. As demonstrated in the code sample, the ParameterFieldInfo property provides access to the report parameters.

Customizing the Viewer's Appearance

The following properties allow you to customize the look of the report viewer by hiding or showing toolbar buttons and other elements:

  • DisplayGroupTree If your report includes groups, they will automatically be displayed to the left of the main report. Clicking a group name navigates to the appropriate page in the report. If your report does not have any groups or you do not want to display the group tree, set this property to False.

  • DisplayToolbar Determines whether the navigation toolbar is displayed.

  • HasGotoPageButton Determines whether the user can enter a page number directly in the toolbar.

  • HasSearchButton The search field and button on the toolbar allow the user to type in a search phrase. The CrystalReportViewer will search forward through the pages of the report and stop on a page containing the phrase.

  • HasPageNavigationButtons Controls whether the four buttons for page navigation are visible.

  • HasZoomFactorList Controls whether the Zoom button is visible, which allows the user to adjust report font size.

  • PagetoTreeRatio Determines how the screen space is split between the group tree and the report page.

As you may have noticed, one major difference in the Web version of the CrystalReportViewer control from its Windows counterpart is the lack of a Print button. The reason is that the Web CrystalReportViewer only delivers HTML to the client browser one page at a time. Printing requires some type of code on the client to be able to retrieve each page and send it to the printer. For certain types of reports, namely those without a lot of fields and records, you may be able to get around this limitation by using the browser's Print option. If you set the SeparatePages property to False, the CrystalReportViewer will return all the data rows on one screen. You can also hide the toolbar and other buttons, and the printout might actually look decent. However, if your report has multiple pages of data, you may have to install the .NET framework on the end user's PC and have her use the Windows version of Crystal Reports.


    Team-Fly    
    Top
     

    Категории