www.codeguru.com/csharp/sample_chapter/article.php/c13037/

Back to Article

Home >> .NET / C# >> Sample Chapter


Web parts: the building blocks of portals
Rating: none

DarrenNeimke (view profile)
December 19, 2006

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.

With the user control in design mode, add a GridView control and choose the <New Data Source> option. Walk through the Data Source Configuration wizard, in a manner similar to the steps we took for the previous control, and bind the GridView to the ListEmployees method of the data layer class. Finally, add the web part to the RightZone in the web part page.

When viewed in the designer, your page should be similar to the page shown in figure 2.19. You can now view the portal by right-clicking on the Default.aspx file and choosing "View in Browser."


(Full Size Image)

Figure 2.19 The portal when viewed in design mode within Visual Studio 2005

The full source code for the portal at this stage can be found in the resources that accompany this book.

2.7 SUMMARY

In this chapter we've covered a stretch of important ground in learning about these fundamental portal components; but perhaps even more important is the fact that we now have our portal up and running. As we move through the book, in each chapter we will add small additional touches to the portal as we learn new concepts. By the end of the last chapter you will see that the sum of all these small additions is an interesting portal with many useful features.

Throughout this chapter we covered some fundamental lessons about web parts and, in particular, the WebPart server control, and we then added them to our own portal. We have demystified some of what happens when controls are added to web zones and learned about the special GenericWebPart control. We also saw how to use verbs to link additional operations to our web parts.

Finally, we started work on the Adventure Works portal application by creating the Visual Studio 2005 project and then adding our first web parts for the HR department. These web parts were simple, but we're not finished with them yet. The next chapter dips into web part connections. With this knowledge we'll be positioned to connect our two web parts and have the department part act as a filter for the employee part. This will allow us to complete the requirement that the employees be viewable by department.

This article is an excerpt from Chapter 2, "Web parts: the building blocks of portals" from ASP.NET Web Parts in Action by Darren Neimke, published by Manning Publications. Reprinted here with the publisher's permission.

Go to page: Prev  1  2  3  4  5  

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed






internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs