Welcome!

.NET Authors: Liz McMillan, Peter Silva, Yakov Werde, Matthew Pollicove , Kevin Benedict

Related Topics: .NET

.NET: Article

Quickstart: Building User-Configurable ASP.NET Web Parts

Understanding ASP.NET Web Parts

Now, for each of the user interface artifacts in the control that we want to be configurable by the end user at runtime, we have to define a Boolean indicator in our control that can be used to decide whether the element should or shouldn't be displayed according to the user's choices.

Each will look like this.

    Protected _PropertyONE As Boolean = True
    <Personalizable(PersonalizationScope.User), WebBrowsable(), _
    WebDisplayName("Show The First Thing"), _
    WebDescription("Use this property to show/hide Thing ONE")> _
    Public Property PropertyONE() As Boolean
      Get
        Return _PropertyONE
      End Get
      Set(ByVal value As Boolean)
        _PropertyONE = value
      End Set
    End Property

The property is marked as Personalizable so that the state of the property will be automatically saved when the property is modified. The property is also marked as WebBrowsable so that it can be modified in a designer like our PropertyGridEditorPart.

By default, the value of a property marked as Personalizable is saved to a Microsoft SQL Server 2005 Express database table located in your application root App_Data folder. You can modify the location where personalization data is saved by modifying your application's Web configuration file.

For the right value to be associated with the right user, the ASP.NET Framework has to be able to identify the current user. You can use personalization with both Windows and Forms Authentication (Windows authentication is the default). When using Forms Authentication, the user must be authenticated before the user requests the page. Since ASP.NET 2.0 implements the "Provider Model" for membership, you can use any available membership provider.

The WebDisplayName and WebDisplayDescription are automatically used in our WebPartEditor. The Boolean value of each property will be set according to the checkbox in the Web Part Properties editor.

Having defined a WebBrowsable property for each of the user interface elements that we want to be configurable by the user, we can simply use those properties before the page loads to set the element's "Visible" property to whatever the user has chosen.

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e
    As System.EventArgs) Handles Me.PreRender
       TextBox1.Visible = PropertyONE
       TextBox2.Visible = PropertyTWO
       TextBox3.Visible = PropertyTHREE
    End Sub

Now our basic Web Part is ready for use, so we can build a simple .ASPX container to test our Web Part.

We'll need an .ASPX page with a WebPartManager, a WebPartZone, and an EditorZone.

In Visual Studio's Solution Explorer we can just drag the .ASCX file of our control and drop it into the WebPartZone.

Then from the WebParts toolbox we can drag a WebPartEditorZone control (see Figure 4) and drop it onto the EditorZone on our page.

Note that the WebPartZone control exposes a rich set of properties that we can use to configure the visual aesthetics of the user experience. For example, in our demo application we've set properties to add color and borders to the default menus displayed when the user is configuring the Web Part.

There is one more bit of functionality that we have to implement before we can run a little application. We need to provide the user with a way to switch in and out of the "Edit Mode."

To do this we'll just include a button on our WebForm and wire up its Click Event Handler to set the DisplayMode property of our WePartManager appropriately.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles Button1.Click
    If Not WebPartManager1.DisplayMode Is _
WebPartManager.EditDisplayMode Then

       WebPartManager1.DisplayMode = WebPartManager.EditDisplayMode
       Button1.Text = "Set Edit Mode Off"
    Else
       WebPartManager1.DisplayMode = WebPartManager.BrowseDisplayMode
       Button1.Text = "Set Edit Mode On"
    End If
End Sub

You're up and running with your first Web Part.

This article is a quick start, but the ASP.NET Web Parts framework is a rich set of technologies. Now that you have a bit of Web Parts development under your belt you're ready to start exploring some more advanced development with it.

Check out the quick start and video training at www.asp.net and begin developing .DLL-based Web Parts using alternate membership providers, adding a Web Parts Catalog and more.

More Stories By Joe Stagner

Joe Stagner joined Microsoft in 2001 as a technical evangelist and is now a Developer Community Champion with the Microsoft MSDN Team. Joe?s technical career began at age 14 with a part-time job as a robotics control programmer, and has visited virtually every genre of technical vocation from Device Driver Developer to President and CEO of a publicly traded New York City consulting firm. Joe?s development experiences have allowed him to create commercial software applications across a wide diversity of technical platforms, from Mainframes through UNIX and Linux, to Microsoft technologies on the Intel and Mobile computing platforms. While Joe is generally interested in all computing technology, in recent years he has been particularly focused on highly performant, geoscalable Web application architectures, multiplatform interoperability, and writing secure code. www.JoeOn.NET

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
.NET News Desk 11/19/06 05:03:11 PM EST

For more than 20 years the software development industry has regarded reuse as the Holy Grail of software development. Programming language-based object-oriented features promised to deliver the significant benefits of increased productivity and cost-effectiveness by creating reusable objects, but in industry-wide practice OO itself hasn't delivered the results we hoped for.