Export (0) Print
Expand All

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 : HierarchicalDataBoundControlDesigner, IDataBindingSchemaProvider
public class MenuDesigner extends HierarchicalDataBoundControlDesigner implements IDataBindingSchemaProvider
public class MenuDesigner extends HierarchicalDataBoundControlDesigner implements IDataBindingSchemaProvider
Not applicable.

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.

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.Design.WebControls;
using System.ComponentModel;
using System.Security.Permissions;
using System.Drawing;

namespace Examples.CS.WebControls.Design
{
    // The MyMenu is a copy of the Menu.
    [AspNetHostingPermission(SecurityAction.Demand, 
        Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, 
        Level = AspNetHostingPermissionLevel.Minimal)]
    [Designer(typeof(Examples.CS.WebControls.Design.MyMenuDesigner))]
    public class MyMenu : Menu
    {
    } // MyMenu

    // Override members of the MenuDesigner.
    public class MyMenuDesigner : MenuDesigner
    {
        // Generate the design-time markup for the control when an error occurs.
        protected override string GetErrorDesignTimeHtml(Exception ex) 
        {
            // Write the error message text in red, bold.
            string errorRendering =
                "<span style=\"font-weight:bold; color:Red; \">" +
                ex.Message + "</span>";

            return CreatePlaceHolderDesignTimeHtml(errorRendering);
        } // GetErrorDesignTimeHtml

        // Generate the design-time markup for the control 
        // when the template is empty.
        protected override string GetEmptyDesignTimeHtml()
        {
            string noElements = "Contains no menu items.";

            return CreatePlaceHolderDesignTimeHtml(noElements);
        } // GetEmptyDesignTimeHtml

        // Generate the design-time markup.
        public override string GetDesignTimeHtml()
        {
            // Make the control more visible in the designer.  If the border 
            // style is None or NotSet, change the border to an orange dotted line. 
            MyMenu myMenuCtl = (MyMenu)ViewControl;
            string markup = null;

            // Check if the border style should be changed.
            if (myMenuCtl.BorderStyle == BorderStyle.NotSet ||
                myMenuCtl.BorderStyle == BorderStyle.None)
            {
                BorderStyle oldBorderStyle = myMenuCtl.BorderStyle;
                Color oldBorderColor = myMenuCtl.BorderColor;

                // Set the design-time properties and catch any exceptions.
                try
                {
                    myMenuCtl.BorderStyle = BorderStyle.Dotted;
                    myMenuCtl.BorderColor = Color.FromArgb(0xFF7F00);

                    // Call the base method to generate the markup.
                    markup = base.GetDesignTimeHtml();
                }
                catch (Exception ex)
                {
                    markup = GetErrorDesignTimeHtml(ex);
                }
                finally
                {
                    // Restore the properties to their original settings.
                    myMenuCtl.BorderStyle = oldBorderStyle;
                    myMenuCtl.BorderColor = oldBorderColor;
                }
            }
            else
                // Call the base method to generate the markup.
                markup = base.GetDesignTimeHtml();

            return markup;

        } // GetDesignTimeHtml

        public override void Initialize(IComponent component)
        {
            // Ensure that only a MyMenu can be created in this designer.
            if (!(component is MyMenu))
                throw new ArgumentException(
                    "The component is not a MyMenu control.");
            
            base.Initialize(component);

        } // Initialize
    } // MyMenuDesigner
} // Examples.CS.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 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft