ControlDesigner.ViewControl 属性

定义

获取或设置可用于预览设计时 HTML 标记的 Web 服务器控件。Gets or sets a Web server control that can be used for previewing the design-time HTML markup.

public:
 property System::Web::UI::Control ^ ViewControl { System::Web::UI::Control ^ get(); void set(System::Web::UI::Control ^ value); };
public System.Web.UI.Control ViewControl { get; set; }
member this.ViewControl : System.Web.UI.Control with get, set
Public Property ViewControl As Control

属性值

基类用于生成设计时 HTML 标记的 Control 对象。A Control object used by the base class to generate design-time HTML markup.

示例

下面的代码示例演示如何使用 SupportsPreviewControlAttribute 特性标记控件设计器。The following code example demonstrates how to mark a control designer with the SupportsPreviewControlAttribute attribute. 此代码示例从 Label 类派生 Web 服务器控件,并将该控件与自定义控件设计器实现相关联。The code example derives a Web server control from the Label class and associates the control with a custom control designer implementation. 控件设计器类声明标记为 SupportsPreviewControl 特性设置为 trueThe control designer class declaration is marked with the SupportsPreviewControl attribute set to true. 控件设计器将重写 GetDesignTimeHtml 方法,然后在设计时以斜体显示控件的 Text 属性。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.
    }
}
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.Design.WebControls
Imports System.Web.UI.WebControls
Imports System.Reflection

Namespace ControlDesignerSamples.VB

    ' 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
        Inherits Label

        ' Use the Label control implementation, but associate
        ' the derived class with the custom control designer.
    End Class


    ' 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
        Inherits TextControlDesigner

        ' Override the base GetDesignTimeHtml method to display 
        ' the design time text in italics.
        Public Overrides Function GetDesignTimeHtml() As String
            Dim html As String = String.Empty

            Try
                ' Get the ViewControl for the associated control.
                Dim ctrl As Label = CType(ViewControl, Label)

                ' Set the default text, if necessary
                If ctrl.Text.Length = 0 Then
                    ctrl.Text = "Sample Text"
                End If

                ' Set the style to italic
                ctrl.Style.Add(HtmlTextWriterStyle.FontStyle, "italic")

                ' Let the base class create the HTML markup
                html = MyBase.GetDesignTimeHtml()
            Catch ex As Exception
                If String.IsNullOrEmpty(html) Then
                    ' Display the exception message
                    html = GetErrorDesignTimeHtml(ex)
                End If
            End Try

            Return html
        End Function

    End Class
End Namespace

注解

ViewControl 属性使用 UsePreviewControl 属性来确定其返回值。The ViewControl property uses the UsePreviewControl property to determine its return value.

如果 trueUsePreviewControl 属性,ViewControl 属性返回控件的临时副本。If the UsePreviewControl property is true, the ViewControl property returns a temporary copy of the control. 对临时控件所做的更改不会持久保存。Changes to the temporary control are not persisted.

如果 falseUsePreviewControl 属性,ViewControl 属性返回控件的 Component 属性的实例。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.

SupportsPreviewControlAttribute 对象中的 SupportsPreviewControl 设置用于设置 UsePreviewControl 属性的值。The SupportsPreviewControl setting in the SupportsPreviewControlAttribute object is used to set the value of the UsePreviewControl property. 因此,SupportsPreviewControl 设置确定基 ControlDesigner 类中 ViewControl 属性返回的控件类型。Therefore, the SupportsPreviewControl setting determines the type of control that is returned by the ViewControl property in the base ControlDesigner class. 如果未在控件设计器声明中指定 SupportsPreviewControlAttribute,则 ControlDesigner 对象行为等效于将 SupportsPreviewControl 属性指定为 falseIf the SupportsPreviewControlAttribute is not specified in the control designer declaration, the ControlDesigner object behavior is equivalent to specifying the SupportsPreviewControl property as false.

适用于

另请参阅