Export (0) Print
Expand All

ControlDesigner.ViewControl Property

Gets or sets a Web server control that can be used for previewing the design-time HTML markup.

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

public Control ViewControl { get; set; }

Property Value

Type: System.Web.UI.Control
A Control object used by the base class to generate design-time HTML markup.

The ViewControl property uses the UsePreviewControl property to determine its return value.

If the UsePreviewControl property is true, the ViewControl property returns a temporary copy of the control. Changes to the temporary control are not persisted.

If the UsePreviewControl property is false, the ViewControl property returns an instance of the Component property for the control. Changes to the instance of the control are persisted.

The SupportsPreviewControl setting in the SupportsPreviewControlAttribute object is used to set the value of the UsePreviewControl property. Therefore, the SupportsPreviewControl setting determines the type of control that is returned by the ViewControl property in the base ControlDesigner class. If the SupportsPreviewControlAttribute is not specified in the control designer declaration, the ControlDesigner object behavior is equivalent to specifying the SupportsPreviewControl property as false.

The following code example demonstrates how to mark a control designer with the SupportsPreviewControlAttribute attribute. The code example derives a Web server control from the Label class and associates the control with a custom control designer implementation. The control designer class declaration is marked with the SupportsPreviewControl attribute set to true. The control designer overrides the GetDesignTimeHtml method, and then displays the Text property of the control in italic at design time.

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.Design.WebControls;
using System.Web.UI.WebControls;
using System.Reflection;

namespace ControlDesignerSamples.CS
{
    // Define a simple designer associated with a  
    // simple text web control. 
	
    // Mark the designer with the SupportsPreviewControlAttribute set 
    // to true.  This means the base.UsePreviewControl returns true, 
    // and base.ViewControl returns a temporary preview copy of the control.
    [SupportsPreviewControl(true)]
    public class SimpleTextControlDesigner : TextControlDesigner
    {		
        // Override the base GetDesignTimeHtml method to display  
        // the design time text in italics. 
        public override string GetDesignTimeHtml()
        {
            string html = String.Empty;

            try
            {
                // Initialize the return string to the default 
                // design time html of the base TextControlDesigner.
                html = base.GetDesignTimeHtml();

                // Get the ViewControl for the associated control.
                Label ctrl = (Label)ViewControl;

                ctrl.Style.Add(HtmlTextWriterStyle.FontStyle, "Italic");
                html = base.GetDesignTimeHtml();

            }
            catch (System.Exception e)
            {
               if (String.IsNullOrEmpty(html))
               {
                   html = GetErrorDesignTimeHtml(e);
               }
            }

            return html;
        }

    }

    // Derive a simple Web control from Label to render a text string. 
    // Associate this control with the SimpleTextControlDesigner.
    [DesignerAttribute("ControlDesignerSamples.CS.SimpleTextControlDesigner"),
    ToolboxData("<{0}:MyLabelControl Runat=\"Server\"><{0}:MyLabelControl>")]
    public class MyLabelControl : Label
    {
        // Use the Label control implementation, but associate 
        // the derived class with the custom control designer.
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft