Manipulating Web Parts in ASP.Net 2.0 - Editing Web Parts
(Page 2 of 4 )
By now you should be pretty comfortable with installing web parts in your web applications. If not, you’ll need to go back to my first article on the subject, located here. You should have a basic page up and running with a couple of web parts, and if you followed the tutorial, they’re most likely based on user controls. Here’s the final code we developed in the last tutorial:
<%@ register tagprefix=”jc” tagname=”Weather” src=”weather.ascx” %>
<%@ register tagprefix=”jc” tagname=”Stocks” src=”stocks.ascx” %>
<%@ page language=”C#” %>
<html>
<head>
<title>webparts tutorial</title>
</head>
<body>
<form runat=”server” id=”Form1”>
<asp:WebPartManager runat=”server” id=”myWebMgr” />
<asp:WebPartZone runat=”server” id=”zone1” PartChromeType=”Title”>
<CloseVerb text=”hide” />
<minimizeverb text=”shrink” />
<PartStyle BackColor=”#CCC” />
<ZoneTemplate>
<jc:Weather runat=”server” id=”myWeather” />
<jc:Stocks runat=”server” id=”myWeather” />
</ZoneTemplate>
</ asp:WebPartZone>
</form>
</body>
</html>
This will display the weather and stock user controls, but with all the added functionality and visual characteristics of a standard web part. The functionality includes being able to minimize, close, and move a web part within a page.
All visual characteristics are of course customizable, by both the developer and the end user. In the last article, we saw only how the programmer determines the visual aspects. But now I’ll show you how the end user can modify these, at run time. They can also make modifications to more than just the display; they can modify some of the behavior as well. Here’s the code you’d need to add to allow users to make their edits.
<asp:EditorZone runat=”server” HeaderText=”Modify Settings”>
<ZoneTemplate>
<asp:AppearanceEditorPart runat=”server” />
<asp:BehaviorEditorPart runat=”server” />
<asp:LayoutEditorPart runat=”server” />
<asp:PropertyEditorPart runat=”server” />
</ZoneTemplate>
</asp:EditorZone>
You could further customize the actual rendering of the EditorZone by modifying the properties of the header and instruction text, as well as many other visual properties, but that is purely aesthetics, so I’ve trimmed down the example to purely functional pieces. What we’re most concerned with is the actual functionality provided by the four possible editor parts.
Here’s an explanation of what each one does. (Note: not all need to be loaded; I simply placed them all in the code for the sake of explanation)
Appearance Editor Part: modifies visual aspects such as width, title, text direction and border settings.
Behavior Editor Part: determines whether personalization is supported, as well as editing and minimization.
Layout Editor Part: determines frame style (normal vs. minimized), which zone the part is in, and the placement index within the specified zone.
Property Grid Editor Part: custom properties. For this to show up, the web part must have public properties with the Personalizable and WebBrowsable attributes set.
So once you’ve set up the Editor Zone and the contained template, each web part on the page will have an ‘Edit’ link attached to the list of options. Once clicked, the editor web part will appear exactly where you’ve coded it on the page. The default “Windows” buttons will appear in the footer, “OK”, “Apply”, and “Cancel”. Once again Microsoft has taken another step towards making the web a comfortable extension of what the vast majority of people are comfortable with in their operating system. This is a smart usability tactic.
So once the user has made a change to their web parts’ settings, what needs to happen next? In other words: what do we as programmers have to develop to retain and restore each persons’ custom settings? Nothing. Absolutely nothing. If we’ve defined the personalization data store (next article), the personalization engine of the ASP.Net framework takes care of absolutely everything for us. Now you understand why I said web parts are going to be huge!
Next: The Web Part Catalog >>
More ASP.NET Articles
More By Justin Cook