ControlDesigner.ViewControl Propriété

Définition

Obtient ou définit un contrôle serveur Web qui peut être utilisé pour afficher un aperçu du balisage HTML au moment du design.

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

Valeur de propriété

Control

Objet Control utilisé par la classe de base pour générer le balisage HTML au moment du design.

Exemples

L’exemple de code suivant montre comment marquer un concepteur de contrôles avec l’attribut SupportsPreviewControlAttribute . L’exemple de code dérive un contrôle serveur Web de la Label classe et associe le contrôle à une implémentation de concepteur de contrôles personnalisé. La déclaration de classe du concepteur de contrôles est marquée avec l’attribut SupportsPreviewControl défini sur true. Le concepteur de contrôles remplace la GetDesignTimeHtml méthode, puis affiche la Text propriété du contrôle en italique au moment de la conception.

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

Remarques

La ViewControl propriété utilise la UsePreviewControl propriété pour déterminer sa valeur de retour.

Si la UsePreviewControl propriété est true, la ViewControl propriété retourne une copie temporaire du contrôle. Les modifications apportées au contrôle temporaire ne sont pas conservées.

Si la propriété est false, la ViewControl UsePreviewControl propriété retourne une instance de la Component propriété pour le contrôle. Les modifications apportées à l’instance du contrôle sont conservées.

Le SupportsPreviewControl paramètre de l’objet SupportsPreviewControlAttribute est utilisé pour définir la valeur de la UsePreviewControl propriété. Par conséquent, le SupportsPreviewControl paramètre détermine le type de contrôle retourné par la ViewControl propriété dans la classe de base ControlDesigner . Si le SupportsPreviewControlAttribute paramètre n’est pas spécifié dans la déclaration du concepteur de contrôles, le ControlDesigner comportement de l’objet équivaut à spécifier la SupportsPreviewControl propriété en tant que false.

S’applique à

Voir aussi