Page view counter

These tutorials demonstrate selected features in ASP.NET version 2.0, but they are compatible with later versions of ASP.NET as well. For the current documentation, see the ASP.NET portal on the MSDN Web site.

 

 

   Welcome   |   ASP.NET   |   Web Services   |   Class Browser   
  |   I want my samples in...      

ASP.NET Quickstart Tutorials

Creating Custom Web Parts

Since any ASP.NET server control can be used as a web part, you can write custom web parts using ASP.NET user controls or custom server controls.

This example shows a user control web part that accepts text input. The user control implements the IWebPart interface to provide values for common display properties of the web part, such as its title and description.

VB User Control Web Part
Run Sample View Source

You can also write web parts as custom server controls. Although you can inherit from any control, ASP.NET provides the WebPart base control class, which makes it significantly easier to customize the default behavior of the control.

This example shows a WebPart control that displays some text. The control has a property called ControlText, which specifies the text to display. It also overrides the AllowMinimize and AllowClose properties to prevent the part from being minimized or closed by the user.

VB Custom Control Web Part
Run Sample View Source

A web part can also provide custom verbs that allow users to take actions on it. The zone is responsible for showing verbs for a web part; in most cases, verbs are shown in the part's Verb Menu. In the earlier editing example, the "Edit" dropdown item was a verb shown only in edit mode.

To provide custom verbs for a control that inherits from WebPart, you can override the Verbs property, create a WebPartVerbCollection object with a collection of WebPartVerb objects, and return it. This example shows how to combine the verbs provided by the base class with a new verb called "Save". The verb will be shown on the Verb Menu of the part.

VB Adding Custom Verbs
Run Sample View Source

A web part can also specify properties that can be edited by the user. To provide editable properties, you can mark the property declaration with the WebBrowsable attribute. The page's EditorZone must also include an editor part capable of editing custom properties. You can use the PropertyGridEditorPart to provide editing for custom properties in a property grid.

This example makes the Text property of the web part editable, and uses a PropertyGridEditorPart to allow users to edit it.
  1. Log in to the page.
  2. Select "Edit" from the dropdown.
  3. Click the dropdown arrow on the control's Verb Menu, and select "Edit" from the menu. The editor will now be visible, with a property grid showing the Text property.
VB Editing Web Part Properties
Run Sample View Source

You can also provide your own custom editor parts for editing part properties. These parts are displayed in the editor zone when the user edits the web part.

To create a custom editor part, inherit from the EditorWebPart control. The EditorWebPart control is a composite control, so you can override the CreateChildControls method, and create controls for the part. You also need to override the ApplyChanges and SyncChanges methods to apply changes made in the editor part to the control.

To provide custom editor parts, you can implement the IWebEditable interface on the part, and implement the CreateEditorParts method and the WebBrowsableObject property. The CreateEditorParts method should return a collection of editor parts.

This example shows a custom editor part to edit a property of a web part.

VB Writing a Custom Editor Part
Run Sample View Source