RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Explore ASP.NET 2.0 Web Part Infrastructure : Page 2

Web Parts can help you build better Web sites. Find out why and learn the ins and outs of building and deploying them.

The Requirements
The best way to learn to swim is to dive right in, but it's always nice to have a shallow pool and a lifeguard watching. Let us do exactly that. With a basic understanding of the various players involved in the ASP.NET 2.0 Web Part infrastructure, let me help you solidify your understanding by creating a Web site driven by the ASP.NET 2.0 Web Part infrastructure. At the end of this article, you will have a reusable Web Parts-based framework similar to the one I used to set up my Web site.

The Web Part framework and the membership API work together to provide an extremely flexible architecture
I created the following list of requirements for my Web site:

  1. Provide an area where I can discuss my latest books, articles, and speaking engagements that I am currently involved with.
  2. Provide a contact form so anyone can contact me without divulging my e-mail address.
  3. Have a content editor widget (Web Part), which I can reuse anywhere on the site for any arbitrary HTML content.
A Bit about Design
This article by no means feigns to be the definitive authority on any software development methodology, but it still makes sense to have some forethought about design. Think of this as sticking your toe into the pool before you dive in, and you just want to make sure there are no sharks in the pool. Throughout this article, my theme will be "laziness" or doing the least to get the job done. In light of that theme, let me talk through the requirements again.

For requirement #1, I wish to have an area where I can discuss my latest books, articles, speaking engagements etc; that sounds like a perfect candidate for an RSS feed. RSS feeds are all over the place, especially on blogs, so it makes sense to stick with my theme of laziness and reuse my current blog. For example, if I have a blog at http://blah.winsmarts.com, I could easily create a category over there and absorb that category's RSS feed in www.winsmarts.com. To do that, I'll write a custom Web Part that I can drop on the surface of my Web Form. On that custom Web Part, I will add a property called RssUrl, which renders the specified RSS feed of that specific category from my blog on my site.

In requirement #2, I want to provide a contact form where users can enter their name, e-mail address, and send me a message directly from my Web site. I could write a Web Part to provide such a form. But this violates the first commandment of this article, "Thou shalt be lazy." Even though I could write an ASP.NET server control, or a Web Part (which is very much like a server control) to satisfy this requirement, it is just much easier to do this as a user control instead. In a user control, you can set up the form's look and feel and various required field and regular expression validators merely by a simple point-and-click operation. Thus this serves as a perfect excuse to demonstrate both my laziness and the extensibility of the ASP.NET 2.0 Web Part infrastructure. Thus I will implement this control as a user control, and use that user control as a Web Part.

For requirement #3, I want to provide a content editor Web Part. I will create a simple Web Part that has a public property called HtmlContent of type string. I can then edit the content inside a single-line textbox. As you will see further in this article, the ability to edit a public property of a Web Part is available to you pretty much out of the box by using the PropertyGridEditorPart. The PropertyGridEditorPart provides you a convenient mechanism to edit public properties of the Web Part decorated with the WebBrowsable (true) attribute. Yes, you could edit complex and fancy HTML in a single-line textbox, but you could also row a tiny canoe all the way to Japan or clean your floor with a toothbrush. These options feel to me like repairing a car engine from the exhaust pipe. Editing complex HTML in a single-line edit box is definitely not convenient.

I will at least provide a TextArea control or, even better, I'll integrate a full-fledged Web-based HTML editor to provide rich HTML editing capability. Thus I can again demonstrate the extensibility of the ASP.NET 2.0 Web Part framework, by integrating the telerik r.a.d. editor.

Thus, in this article, I will show you how to write three Web Parts:

  1. A Web Part that absorbs an RSS feed and displays it on the Web site.
  2. A user control wrapped as a Web Part that provides a simple contact form with some basic validation.
  3. A Web Part that allows the user to maintain the static content of the Web site as HTML. This Web Part will also involve writing a custom editor using telerik's r.a.d. editor to give the user a convenient area to type in HTML content.
In addition to these Web Parts, I'll show you how to write a simple framework to host Web Parts based on the ASP.NET 2.0 Web Part framework. Once you have such a framework ready, you can easily extend this system and add more features by simply writing more Web Parts.

I obviously want to provide access protection to my Web site. Thus I'll also create a login page to enable the "editability" of the Web site. For anonymous users, however, the content appears as read only. To do this, I'll leverage the ASP.NET 2.0 framework membership API. The Web Part framework and the membership API work together to provide an extremely flexible architecture. Here's how to set up the framework.

Setting Up the Web Part Framework
Figure 1 shows what the eventual site will look like. As you can see, this Web site has a default view for anonymous users, which presents the content, but does not allow the user to edit the content.

Figure 1: The site when it is fully done.
Figure 2: The site in edit mode, when it is fully done.
Also, as shown in Figure 2, when registered users log into the Web site, they'll see a pane on the right that allows them to perform Web site administration tasks. You can see a menu at the top and an "Editor Zone" that holds the telerik r.a.d. editor along with other editable properties. I'll explain these in detail shortly.

To set up the common look and feel for the Web site, I'll create a master page to define both the UI and some common logic for the entire Web site.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date