WebPartDescription Class


Provides information about a WebPart control that you can display in a catalog of Web Parts controls without having to create an instance of the control.

Namespace:   System.Web.UI.WebControls.WebParts
Assembly:  System.Web (in System.Web.dll)


Public Class WebPartDescription

System_CAPS_pubmethodWebPartDescription(String, String, String, String)

Initializes a new instance of the class by using several strings that contain description information for a WebPart control.


Initializes a new instance of the class when a WebPart control instance is available.


Gets a URL containing the path to an image used as an icon for a WebPart control.


Gets the text of a description for a WebPart control.


Gets the ID of a corresponding WebPart control.


Gets the title text of a corresponding WebPart control.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

When WebPart controls are displayed in a catalog of controls that users can add to a page, some basic information about each control is required. For example, it is useful to have a title for the control, and a description, so that as users view a catalog they have enough information to decide whether to add a control to the page. However, a catalog of WebPart controls can potentially contain many controls, and it could affect the performance of an application if an instance of every WebPart control must be created to extract the information to display in the catalog.

The WebPartDescription class exists so that it is not necessary to create an instance of a WebPart control to retrieve the information about the control that is displayed in a catalog of controls. In the Web Parts control set, a WebPartDescription object is also used in conjunction with the various CatalogPart controls when a page is in catalog display mode.

The WebPartDescription class has two overloads of its constructor, one that takes a WebPart control as a parameter when an instance is available (the WebPartDescription constructor), and one that takes several strings with information about the control as parameters (the WebPartDescription constructor).

The WebPartDescription class also has several properties designed to contain the description information for WebPart controls. The following table summarizes the WebPartDescription properties, and what property each one corresponds to in a WebPart control.

Description property

Related part control property









The following code example demonstrates programmatic use of the WebPartDescription class. Ordinarily, this type is used primarily by the Web Parts control set, but this code example is simply showing basic programmatic usage of the main description properties.

The code example has four parts:

  • A user control that enables users to change display modes on a Web page.

  • A custom WebPart control.

  • A Web page to host the other controls.

  • An explanation of how the code example works.

The first part of the code example is the user control. The source code for the user control comes from another topic. For more details about the user control, see the topic Walkthrough: Changing Display Modes on a Web Parts Page.

<%@ control language="vb" classname="DisplayModeMenuVB"%>
<script runat="server">
  ' Use a field to reference the current WebPartManager.
  Dim _manager As WebPartManager

  Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
    AddHandler Page.InitComplete, AddressOf InitComplete
  End Sub

  Sub InitComplete(ByVal sender As Object, ByVal e As System.EventArgs)
    _manager = WebPartManager.GetCurrentWebPartManager(Page)

    Dim browseModeName As String = WebPartManager.BrowseDisplayMode.Name

    ' Fill the dropdown with the names of supported display modes.
    Dim mode As WebPartDisplayMode
    For Each mode In _manager.SupportedDisplayModes
      Dim modeName As String = mode.Name
      ' Make sure a mode is enabled before adding it.
      If mode.IsEnabled(_manager) Then
        Dim item As New ListItem(modeName, modeName)
      End If
    Next mode

    ' If shared scope is allowed for this user, display the scope-switching
    ' UI and select the appropriate radio button for the current user scope.
    If _manager.Personalization.CanEnterSharedScope Then
      Panel2.Visible = True
      If _manager.Personalization.Scope = PersonalizationScope.User Then
        RadioButton1.Checked = True
        RadioButton2.Checked = True
      End If
    End If

  End Sub

  ' Change the page to the selected display mode.
  Sub DisplayModeDropdown_SelectedIndexChanged(ByVal sender As Object, _
    ByVal e As EventArgs)

    Dim selectedMode As String = DisplayModeDropdown.SelectedValue   
    Dim mode As WebPartDisplayMode = _
    If Not (mode Is Nothing) Then
      _manager.DisplayMode = mode
    End If

  End Sub

  ' Set the selected item equal to the current display mode.
  Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
    Dim items As ListItemCollection = DisplayModeDropdown.Items
    Dim selectedIndex As Integer = _
    DisplayModeDropdown.SelectedIndex = selectedIndex

  End Sub

  ' Reset all of a user's personalization data for the page.
  Protected Sub LinkButton1_Click(ByVal sender As Object, _
    ByVal e As EventArgs)


  End Sub

  ' If not in User personalization scope, toggle into it.
  Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, _
    ByVal e As EventArgs)

    If _manager.Personalization.Scope = PersonalizationScope.Shared Then
    End If

  End Sub

  ' If not in Shared scope, and if user is allowed, toggle the scope.
  Protected Sub RadioButton2_CheckedChanged(ByVal sender As Object, _
    ByVal e As EventArgs)

    If _manager.Personalization.CanEnterSharedScope AndAlso _
      _manager.Personalization.Scope = PersonalizationScope.User Then
    End If

  End Sub

  <asp:Panel ID="Panel1" runat="server" 
    Font-Names="Verdana, Arial, Sans Serif" >
    <asp:Label ID="Label1" runat="server" 
      Text="&nbsp;Display Mode" 
    <asp:DropDownList ID="DisplayModeDropdown" runat="server"  
      OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" />
    <asp:LinkButton ID="LinkButton1" runat="server"
      Text="Reset User State" 
      ToolTip="Reset the current user's personalization data for the page."
      OnClick="LinkButton1_Click" />
    <asp:Panel ID="Panel2" runat="server" 
      GroupingText="Personalization Scope"
      Visible="false" >
      <asp:RadioButton ID="RadioButton1" runat="server" 
        GroupName="Scope" OnCheckedChanged="RadioButton1_CheckedChanged" />
      <asp:RadioButton ID="RadioButton2" runat="server" 
        OnCheckedChanged="RadioButton2_CheckedChanged" />

The second part of the code example is a custom WebPart control. For the code example to run, you must compile this source code. You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. This code example uses the approach of dynamic compilation. For a walkthrough that demonstrates how to compile, see Walkthrough: Developing and Using a Custom Web Server Control.

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Namespace Samples.AspNet.VB.Controls

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class TextDisplayWebPart
    Inherits WebPart
    Private _contentText As String = Nothing
    Private _fontStyle As String = Nothing
    Private input As TextBox
    Private DisplayContent As Label
    Private lineBreak As Literal

    <Personalizable(), WebBrowsable()> _
    Public Property ContentText() As String
        Return _contentText
      End Get
      Set(ByVal value As String)
        _contentText = value
      End Set
    End Property

    Protected Overrides Sub CreateChildControls()
      DisplayContent = New Label()
      DisplayContent.BackColor = Color.LightBlue
      DisplayContent.Text = Me.ContentText

      lineBreak = New Literal()
      lineBreak.Text = "<br />"

      input = New TextBox()
      Dim update As New Button()
      update.Text = "Set Label Content"
      AddHandler update.Click, AddressOf Me.submit_Click

    End Sub

    Private Sub submit_Click(ByVal sender As Object, _
                             ByVal e As EventArgs)
      ' Update the label string.
      If input.Text <> String.Empty Then
        _contentText = input.Text + "<br />"
        input.Text = String.Empty
        DisplayContent.Text = Me.ContentText
      End If

    End Sub

  End Class

End Namespace

The third part of the code example is the Web page. Near the top are two Register directives: one that registers the user control, and the other to register the custom WebPart control whose source file is located in the App_Code folder of your site. The page contains an <asp:catalogzone> element, which in turn contains declarative references to two controls: the custom WebPart control named TextDisplayWebPart, and a BulletedList Web server control that will be treated as a WebPart control at run time because the WebPartManager control will wrap it with a GenericWebPart object. Note that in the code for the Button1_Click method, the available WebPartDescription objects for the WebPart controls in the catalog are retrieved using the GetAvailableWebPartDescriptions method, and then the description details are all written out to the page.

<%@ Page Language="vb" %>
<%@ Register TagPrefix="uc1"
    Src="~/DisplayModeMenuVB.ascx" %>
<%@ Register TagPrefix="aspSample" 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)

    Label1.Text = String.Empty

    Dim descriptions As WebPartDescriptionCollection = _

    Dim desc As WebPartDescription

    For Each desc In descriptions
      Label1.Text += "ID: " & desc.ID & "<br />" & _
        "Title: " & desc.Title & "<br />" & _
        "Description: " & desc.Description & "<br />" & _
        "ImageUrl: " & desc.CatalogIconImageUrl & "<br />" & _
        "<hr />"

  End Sub

  Protected Sub Page_PreRender(ByVal sender As Object, _
    ByVal e As System.EventArgs)

    If mgr1.DisplayMode Is WebPartManager.CatalogDisplayMode Then
      Button1.Visible = True
      Label1.Visible = True
      Button1.Visible = False
      Label1.Visible = False
    End If

  End Sub


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>Untitled Page</title>
  <form id="form1" runat="server">
    <asp:WebPartManager ID="mgr1" runat="server">
    <uc1:DisplayModeMenuVB ID="menu1" runat="server" />
    <asp:WebPartZone ID="WebPartZone1" runat="server">
        <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
    <asp:CatalogZone ID="CatalogZone1" runat="server">
        <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server">
            <aspSample:TextDisplayWebPart ID="txtDisplayWebPart1" runat="server"
              Title="Text Display"
              Description="Displays entered text in a label control."
              Title="Favorite Links"
              Description="My Favorite Links">
              <asp:ListItem Value="http://msdn.microsoft.com">
              <asp:ListItem Value="http://www.asp.net">
              <asp:ListItem Value="http://www.msn.com">
    <hr />
    <asp:Button ID="Button1" runat="server" 
      Text="List WebPartDescription Info" OnClick="Button1_Click" /> 
    <br />
    <asp:Label ID="Label1" runat="server" Text="" />

After you load the page in a browser, use the Display Mode drop-down list control and select Catalog to change the page to catalog display mode. In the catalog, you should see the two controls that are available to be added to the page. Click the List WebPartDescription Information button, and the code writes out the values of all available WebPartDescription objects to the page. This demonstrates that you can retrieve description details for WebPart controls in a catalog without having to create instances of the controls themselves.

.NET Framework
Available since 2.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top