This documentation is archived and is not being maintained.

MenuDesigner Class

Provides design-time support in a visual designer for the Menu control.

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

Public Class MenuDesigner _
	Inherits HierarchicalDataBoundControlDesigner _
	Implements IDataBindingSchemaProvider
Dim instance As MenuDesigner

The Menu class provides a hierarchical menu Web server control.

In a visual designer, when you switch from Source to Design view, the markup source code that describes the associated Menu control is parsed and a design-time version of the control is created on the design surface. When you switch back to Source view, the design-time control is persisted to markup and added to the existing markup for the Web page. The MenuDesigner class provides design-time support for Menu controls in a visual designer.

The ActionLists property returns a DesignerActionListCollection object, which typically contains an object that is derived from the DesignerActionList class for each level in the inheritance tree of the designer. The AutoFormats property returns a collection of formatting schemes for display in the Auto Format dialog box.

The TemplateGroups property returns a collection of template groups for the templates of the associated Menu control. The UsePreviewControl property always returns true, indicating that the designer creates a temporary copy of the associated Menu to generate the design-time markup.

The MenuDesigner class methods provide the following functionality:

  • The Initialize method prepares the designer to view, edit, and design the associated Menu control. The GetDesignTimeHtml method returns the markup that is used to render the associated Menu at design time.

  • The GetEmptyDesignTimeHtml method gets the markup that renders a placeholder for the associated control at design time when no markup is otherwise available. The GetErrorDesignTimeHtml method provides the markup that renders the associated control at design time when an error has occurred.

  • The DataBind method binds the associated Menu control to the design-time data source. The GetSampleDataSource method constructs a sample data source that can be used at design time for the associated control.

The following code example shows how to extend the MenuDesigner class to change the appearance of controls that are derived from the Menu control at design time.

The example derives the MyMenu class from the Menu. The MyMenu class is a copy of the Menu. The example also derives the MyMenuDesigner class from the MenuDesigner class, and then applies a DesignerAttribute attribute for the MyMenuDesigner on the MyMenu class.

The MyMenuDesigner overrides the following MenuDesigner members:

  • The GetDesignTimeHtml method to draw an orange, dotted border around the control to make its extent more visible.

  • The GetErrorDesignTimeHtml method to generate the markup for a placeholder that includes the error message, which is rendered in red, bold text.

  • The GetEmptyDesignTimeHtml to generate the markup for a placeholder that contains a message indicating that no menu items are defined.

  • The Initialize method to throw an ArgumentException exception, if the associated control is not a MyMenu object.

Imports System
Imports System.Web
Imports System.Web.UI.WebControls
Imports System.Web.UI.Design.WebControls
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Drawing

Namespace Examples.VB.WebControls.Design

    ' The MyMenu is a copy of the Menu.
    <AspNetHostingPermission(SecurityAction.Demand, _
        Level:=AspNetHostingPermissionLevel.Minimal)> _
    <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
        Level:=AspNetHostingPermissionLevel.Minimal)> _
    <Designer(GetType(Examples.VB.WebControls.Design.MyMenuDesigner))> _
    Public Class MyMenu
        Inherits Menu
    End Class ' MyMenu

    ' Override members of the MenuDesigner. 
    Public Class MyMenuDesigner
        Inherits MenuDesigner

        ' Generate the design-time markup for the control when an error occurs. 
        Protected Overrides Function GetErrorDesignTimeHtml( _
            ByVal ex As Exception) As String 

            ' Write the error message text in red, bold. 
            Dim errorRendering As String = _
                "<span style=""font-weight:bold; color:Red; "">" & _
                ex.Message & "</span>" 

            Return CreatePlaceHolderDesignTimeHtml(errorRendering)

        End Function ' GetErrorDesignTimeHtml

        ' Generate the design-time markup for the control  
        ' when the template is empty. 
        Protected Overrides Function GetEmptyDesignTimeHtml() As String 

            Dim noElements As String = "Contains no menu items." 

            Return CreatePlaceHolderDesignTimeHtml(noElements)

        End Function ' GetEmptyDesignTimeHtml

        ' Generate the design-time markup. 
        Public Overrides Function GetDesignTimeHtml() As String 

            ' Make the control more visible in the designer.  If the border  
            ' style is None or NotSet, change the border to an orange dotted line.  
            Dim myMenuCtl As MyMenu = CType(ViewControl, MyMenu)
            Dim markup As String = Nothing 

            ' Check if the border style should be changed. 
            If (myMenuCtl.BorderStyle = BorderStyle.NotSet Or _
                myMenuCtl.BorderStyle = BorderStyle.None) Then 

                Dim oldBorderStyle As BorderStyle = myMenuCtl.BorderStyle
                Dim oldBorderColor As Color = myMenuCtl.BorderColor

                ' Set the design-time properties and catch any exceptions. 
                    myMenuCtl.BorderStyle = BorderStyle.Dotted
                    myMenuCtl.BorderColor = Color.FromArgb(&HFF7F00)

                    ' Call the base method to generate the markup.
                    markup = MyBase.GetDesignTimeHtml()

                Catch ex As Exception
                    markup = GetErrorDesignTimeHtml(ex)

                    ' Restore the properties to their original settings.
                    myMenuCtl.BorderStyle = oldBorderStyle
                    myMenuCtl.BorderColor = oldBorderColor
                End Try 

                ' Call the base method to generate the markup.
                markup = MyBase.GetDesignTimeHtml()
            End If 

            Return markup

        End Function ' GetDesignTimeHtml

        Public Overrides Sub Initialize(ByVal component As IComponent)

            ' Ensure that only a MyMenu can be created in this designer.  
            If Not TypeOf component Is MyMenu Then 
                Throw New ArgumentException( _
                    "The component is not a MyMenu control.")
            End If 


        End Sub ' Initialize
    End Class ' MyMenuDesigner
End Namespace ' Examples.VB.WebControls.Design

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0