Go to page:
Prev 1 2 3 4 5
2.6 ADDING WEB PARTS TO THE ADVENTURE WORKS SOLUTION
In chapter 1 we created a data layer so that we could connect to SQL Server 2005 and retrieve information about the Adventure Works Cycles business. In this chapter we'll start putting that data to good use as we build the beginnings of a portal application based on the Adventure Works business. The portal that we will build throughout the course of this book will be built with small incremental steps. At the end of each chapter we'll apply a concept we've learned by integrating an implementation of that concept into the portal. While each step may, in itself seem small, by the end of the book we will have created a portal that is filled with the features that clients have come to expect of portal-style applications.
(continued)To implement the concepts you've learned in this chapter, let's get back to your job at Adventure Works. Today the HR department has asked you to develop a small website that displays some of their line of business data—such as a list of employees, departments, and information about the latest job candidates. They've specified that initially the portal should be able to display the following data:
- A listing of all departments with employee numbers listed against each one
- A listing of all employees for a given department
As an applications developer for Adventure Works you've got ASP.NET 2.0 installed and you are all geared up and ready for the task.
After discussions with the users, it is clear to you that while they have an immediate need for just these few features, their longer-term requirements are likely to be much larger. For this reason, you make the decision to use the web portal framework to build features as standalone components. Over time there will be the ability to harness the extensibility of the framework through features such as web part connections and verbs, to leverage components that we build today into tomorrow's features.
NOTE To complete this exercise you will need to create a project for the Adventure Works application. If you are comfortable with project creation and some of the new ASP.NET 2.0 features such as master pages, themes, etc., then you might just want to grab the project from the resources for this chapter that come with the book. If you would like to create the project for yourself to see how to implement these new features, you can complete the walkthrough titled "Creating the Adventure Works Project" in the appendix.
Displaying all departments
We'll address the first feature request, which was to create a web part that displays a listing of all departments from the Adventure Works database. Open the Adventure Works project and create a new folder named WebParts and add to it a new user control file named DepartmentListingPart.ascx.
With the user control in design mode, add a GridView server control from the toolbox by dragging it onto the design surface. From the associated GridView tasks, choose the <New Data Source> option so that we can configure a data source to return the data that we need. At this time the Data Source Configuration Wizard starts up and the Choose a Data Source Type screen is displayed as shown in figure 2.15. We've already created a data access layer to perform our data operations, so from this screen we choose the Object data source type and press OK.

Figure 2.15 The first step in the data source configuration wizard is to specify what type of data source we are binding to.
The next screen in the wizard displays a listing of classes, allowing us to choose which business object contains the method to bind to the GridView control. This screen is displayed in figure 2.16. Select the AW.Portal.Data.DataLayer class and then press OK.

(Full Size Image)
Figure 2.16 When using the object data source control, we get to specify which class will provide the data.
With the business object chosen, all that remains is to choose which method of the object will provide us with the data. The last screen of the wizard that we'll be using allows us to select the ListDepartments method and press Finish. This last screen is displayed in figure 2.17.

(Full Size Image)
Figure 2.17 The wizard allows us to choose which methods of the object data source will perform data operations such as Select, Update, Insert, and Delete.
Now the wizard has all of the information it needs to create a data source control that can be bound to the GridView, and we can create a page in which to display our control. To add this web part to the web part page we created earlier, switch the Default.aspx page into design mode and drag the DepartmentListingPart user control from the Server Explorer on to the WebPartZone. Listing 2.16 shows that by adding the user control, Visual Studio has automatically registered the control with the page and added the correct markup for the user control into the zone template for us.
Listing 2.16 The DepartmentListingPart Added Register declaration for user control

(Full Size Image)
The register tag that was added by Visual Studio is known as the @ Register directive. This directive is included in ASP.NET web pages so that a tagname and tagprefix can be associated with user controls and custom server controls. You can see that the tagname (DepartmentListingPart) and tagprefix (uc1) could then be useful in declaring the department listing user control within the page.
We can now run the application and see that the web part is displayed with a listing of departments as shown in figure 2.18. To do this, rightclick on the Default.aspx file and choose "View in Browser."

Figure 2.18 The DepartmentListing web part shows a listing of the departments within the Adventure Works business with the number of employees shown against each department.
Creating an Employees web part
Now that we have a listing of all departments, we may turn our attention to the second requirement we were given—to display a listing of employees for a given department. We'll again create this listing as a web part and again be using the GridView control to display the data in a list. Right-click on the WebParts folder and add to it a new user control file named EmployeeListingPart.ascx.