ASP .NET Applications

book list add book to my bookshelf create a bookmark purchase this book online

mastering crystal reports 9
Chapter 21 - .NET Applications
Mastering Crystal Reports 9
by Cate McCoy and Gord Maric
Sybex 2003

Microsoft didn’t stop with changing Visual Basic and creating the new language C# when they introduced the .NET Framework—they also greatly enhanced the Active Server Page platform and integrated it into the .NET language family. You can now choose to develop web code in any of the .NET languages, whether you are a VB syntax guru or a C# type of coder.

The method of developing web applications is also much easier and is similar to desktop application development. You can now design web pages by dragging user interface elements onto a canvas, sizing and positioning them dynamically, and setting properties in the Visual Studio Property Browser, much like Windows Forms applications are designed.

The Web Forms version of the Crystal Report Viewer does behave a bit differently than its Windows counterpart but is every bit as functional. Also, as is the case in other aspects of ASP .NET versus desktop .NET development, the underlying object model for interfacing with Crystal Reports is identical under both platforms, meaning the end of learning two APIs. Since you’ve already become familiar with the ReportDocument class in .NET desktop development, you have a big head start on hooking up Crystal Reports in the ASP .NET world.

Adding a Report to a Web Page

To embed a report onto a web page, follow the steps below:

  1. Create a new ASP .NET application. Select your favorite .NET language.

  2. Open the default page in the application, named WebForm1.aspx.

  3. Drag a CrystalReportViewer control onto the web page. You should see a gray descriptive box, as shown in Figure 21.8.

    click to expand
    Figure 21.8. Adding a Crystal Viewer to a web page

  4. To bind the viewer to a report, click the button next to the Databindings property in the Property Inspector. You will see a dialog like the one shown in Figure 21.9. Select the ReportSource property from the Bindable Properties list, click the Custom Binding Expression button on the lower right, and then type in the name and location of the desired RPT file on your hard drive. Make sure that the web server has rights to the folder you specify and that you include double quotes around the name. Also note that the screenshot below uses double backslashes in the filename because this is a C# example. You would not double the backslashes if your project were a VB .Net project.

    click to expand
    Figure 21.9. Binding a report to the viewer

If you specify the report filename successfully, you should see the actual report data in the viewer, while still in Design mode, as shown in Figure 21.10.

click to expand
Figure 21.10. Viewing the report data

To complete the viewer code, go into the code-behind file for WebForm1.ascx, find the OnInit method call (you may have to expand the Visual Studio-generated code), and add a DataBind method to the end of the routine. The completed routine should look like this:

override protected void OnInit(EventArgs e)       {       //       // CODEGEN: This call is required by the ASP.NET Web Form Designer.       //          InitializeComponent();          base.OnInit(e);          DataBind();       }

You can now compile and run your application and see the report in your web browser.

Note 

In a Visual Basic .NET project, you would add the Databind method to the existing Page_Init procedure, as opposed to OnInit.

Loading Web Reports on the Fly

As with Windows applications, embedding the report name in the web page is handy, but loading the report on the fly could be even more so. As you can imagine, like the ASP-based generic report viewer that we created in Chapter 19, “Building Windows Applications with the Report Designer Component,” it would be pretty easy to create a similar reporting engine in ASP .NET code.

One of the best parts about ASP .NET development is that the object model between Windows Forms and Web Forms is often identical, meaning that if you’ve learned how to do something on one platform, then there’s a good chance that you can do it on the other platform as well.

Listing 21.3 shows a piece of C# code that loads a report into a web-based CrystalReportViewer control at runtime.

Listing 21.3: C# Loading of a Crystal Report at Runtime

start example
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using CrystalDecisions.CrystalReports.Engine; namespace CrystalDotNet {    /// <summary>    /// Summary description for OnTheFly.    /// </summary>    public class OnTheFly : System.Web.UI.Page    {       protected CrystalDecisions.Web.CrystalReportViewer aViewer;       protected ReportDocument oDoc;           private void Page_Load(object sender, System.EventArgs e)       {          // Put user code to initialize the page here          ReportDocument oDoc = new ReportDocument();          oDoc.Load("c:\\tag\\crystal9\\ch02.rpt");          aViewer.ReportSource = oDoc;       }       … Visual Studio auto-generated code removed… }
end example

Other than the syntax, this should look very similar to the desktop examples shown earlier. A ReportDocument variable is declared and a report is loaded into it. That variable then becomes the ReportSource value for the viewer. One useful implementation of this technique might be to create a menu of available reports that are loaded dynamically as the user selects the desired report from the menu.

If you’re thinking ahead, you might already be asking if this ReportDocument object is customizable in the same ways that it was in the Windows Forms world. The answer is yes!

Customizing the Report at Runtime

The ReportDocument object that was used in the previous section is the same ReportDocument object used in the Windows Forms examples, meaning that the coder can use it to customize the Crystal report at runtime using all the same methods. Listing 21.4 shows the ASP .NET, C# version of the code shown earlier to turn all of the FieldObject and TextObject elements in the report to an italic, red font.

Listing 21.4: Runtime Report Object Customization

start example
using System using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; namespace CrystalDotNet {    /// <summary>    /// Summary description for OnTheFly.    /// </summary>       public class OnTheFly : System.Web.UI.Page       {          protected CrystalDecisions.Web.CrystalReportViewer aViewer;          protected ReportDocument oDoc;    private void Page_Load(object sender, System.EventArgs e)          {             // Put user code to initialize the page here             oDoc = new ReportDocument();             oDoc.Load("c:\\tag\\crystal9\\ch02.rpt");             MakeItRed();             aViewer.ReportSource = oDoc;          }    private void MakeItRed()          {             Font f = new Font("Arial", 8, FontStyle.Italic);             foreach (Section oSec in oDoc.ReportDefinition.Sections)                foreach (ReportObject o in oSec.ReportObjects)                {                   switch (o.Kind)                   {                    case ReportObjectKind.FieldObject:                        (o as FieldObject).Color = Color.Red;                  (o as FieldObject).ApplyFont(f);                  break;                    case ReportObjectKind.TextObject:                        (o as TextObject).Color = Color.Red;                  (o as TextObject).ApplyFont(f);                  break;                   }                }           }           … Visual Studio auto-generated code removed…        } }
end example

Other than the preponderance of curly braces, this code should look pretty familiar. It takes the ReportDocument object and loops through each section, and each ReportObject within each section, hunting down FieldObject and TextObject instances, and changes the font and color of each one.

Report Caching

Crystal Reports can take advantage of the caching mechanism built into ASP .NET projects, meaning that you can improve the reports used by many users. To take advantage of this caching ability, however, the report must be brought directly into your project. Let’s see how to do this.

Just as in Windows Forms applications, you can add any RPT file to your ASP .NET project. Simply right-click the project name in the Solution Explorer, select Add > Add Existing Item, and navigate to an available RPT file. The report will then be shown in the Solution Explorer as part of the project. If you then click Show All Files at the top of the Solution Explorer, you will see that a CS file (or a VB file, depending on what language your project is based on) has been automatically generated. An examination of this file will show that two wrapper classes have been created based on the report, as shown here:

public class CH7 : ReportClass { ... }            public class CachedCH7 : Component, ICachedReport { ... }

The details of this class are not important, as you cannot change any of the code in this auto- generated module. What is important to see is how the second class implements the ICachedReport interface, meaning that it has provided ASP .NET caching functionality.

This second, cacheable class is the one that you will use in your web page. An instance of this class can be used as the ReportSource for a CrystalReportViewer, as shown in Listing 21.5.

Listing 21.5: Using a Cached Report Class in a Viewer

start example
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace CrystalDotNet {    /// <summary>    /// Summary description for CachedReport.    /// </summary>    public class CachedReport : System.Web.UI.Page    {    protected CrystalDecisions.Web.CrystalReportViewer aViewer;    protected System.Web.UI.WebControls.Label lbNote;        protected  CachedCH7 oRpt;          private void Page_Load(object sender, System.EventArgs e)       {          // Put user code to initialize the page here          oRpt = new CachedCH7();          aViewer.ReportSource = oRpt;       }       … Visual Studio auto-generated code removed…    } }
end example

Interactivity with the Report Viewer

The Viewer control provides a number of events that you can use to enhance the interactivity of report navigation. Two such event examples are shown below. The Drill event is fired whenever the user drills down into the report. The parameters passed into the Drill event allow you to determine exactly where the user is drilling. The event code below updates a label control’s contents with the name and level of the group the user just clicked on:

private void aViewer_Drill(object source, CrystalDecisions.Web.DrillEventArgs e)    {       string s;       s = "you clicked on group " + e.NewGroupName;       s += ", level " + e.NewGroupLevel;       lbOut.Text = s;    }

Another event available in the viewer is the ViewZoom event, called whenever the user changes the Zoom level. The simple example below updates a label with the old and new zoom levels if the zoom is 50 percent or above and prevents the user from zooming below 50 percent:

private void aViewer_ViewZoom(object source, CrystalDecisions.Web.ZoomEventArgs e)    {       if (e.NewZoomFactor < 50)       {          lbOut.Text = "zooming too small disallowed";           e.Handled = true;       } else          lbOut.Text = "zoomed from " + e.CurrentZoomFactor + " to " + e.NewZoomFactor;    }

Use of content on this site is expressly subject to the restrictions set forth in the Membership Agreement
 
Conello © 2000-2003     Feedback


Mastering Crystal Reports 9
Mastering Crystal Reports 9
ISBN: 0782141730
EAN: 2147483647
Year: 2005
Pages: 217

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