Adding Dynamic Content
I mentioned yesterday that I've been creating numerous different web parts throughout the course of writing my book on the ASP.NET 2.0 Portal Framework. The web part that I showed in yesterday's article was a part that allowed arbitrary chunks of HTML to be added as content.
Many of the web parts that are included in the book were inspired from web parts that I needed for the next version of SingleUserBlog - which is built on top of the portal framework. Having web parts for all of the dynamic content in SingleUserBlog really gives me a lot of flexibility to add dynamic content to my blog and position it wherever I want.
Hyperlinks WebPart
It would be fair to say that most bloggers have many links that are displayed on their blogs. Heck, even on my simple little blog I have the following groups of links:
My Websites
Blogs I Read
Blogs Using SUB
My Articles
Tools I Use
For other bloggers that I know of it's common to have as many as twenty different categories of links displayed somewhere on the page of their blog. For this reason, one of the first web parts that I built was a web part that allowed me to add hyperlinks and have them displayed on the page. Here's a picture of the web part with links for the blogs that I read:
Custom EditorPart for the HyperlinksWebPart
When I created the HyperlinksWebPart I decided that the data type for storing the custom content should be a custom class that provided a high level of fidelity over how the links are handled. For example, a user should be able to specify some of following attributes for each hyperlink that they add:
- Display Title
- Url
- Description (Optional)
- Display Order
- Whether or not to open in a new page
Using a complex data type to store the personalization sure provides an easy way to store the data about each hyperlink but complex types do not, by default, get displayed within the property editor when editing a web part. Thankfully the portal framework makes it very simple to associate custom user interface elements with a web part by simply overriding a method named CreateEditorParts and returning a class that knows how to create an editing interface for a particular type of web part. By doing this you are able to provide a totally custom solution for managing the data for a web part. For example, you could display a visual tool such as a map to allow a user to choose which region to display weather information for in a weather web part instead of having to manually type a location name.
The following image shows the user interface that I created for the Hyperlinks web part so that I could easily add new links or delete existing ones:
The custom EditorPart that I created is titled "Manage Links" and sits within the existing EditorZone. All of the existing EditorParts that ship with ASP.NET 2.0 can also be displayed within the EditorZone to allow users to manage more "generic" web part properties such as its title, height, width, and chrome type to use.
As you can see, providing custom EditorParts to manage the data in web parts is a great way to make web parts easier for users to manage by providing fields for each property on the custom data type and allowing users to easily delete existing links.
Within the next day or so I'll upload a recent build of SUB V2 and supply the authorization credentials so that people can login and have a play with the new web parts to see how they work for real.