共用方式為


ControlDesigner.ViewControl 屬性

定義

取得或設定 Web 伺服器控制項,可用於預覽設計階段的 HTML 標記。

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

屬性值

Control 物件,可供基底類別產生設計階段 HTML 標記。

範例

下列程式代碼範例示範如何使用 屬性標記控件設計工具 SupportsPreviewControlAttribute 。 程式代碼範例會從 類別衍生 Web 伺服器控制項, Label 並將控件與自定義控件設計工具實作產生關聯。 控件設計工具類別宣告會標示 SupportsPreviewControl 為 屬性 true。 控件設計工具會 GetDesignTimeHtml 覆寫 方法,然後在設計時間以斜體顯示 Text 控件的 屬性。

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

備註

屬性 ViewControlUsePreviewControl 使用 屬性來判斷其傳回值。

UsePreviewControl如果 屬性為 true,則 ViewControl 屬性會傳回 控件的暫存複本。 暫存控件的變更不會保存。

UsePreviewControl如果 屬性為 false,則 ViewControl 屬性會傳回 控件之 屬性的Component實例。 控件實例的變更會保存。

SupportsPreviewControl物件中的SupportsPreviewControlAttribute設定是用來設定 屬性的值UsePreviewControl。 因此,設定SupportsPreviewControl會決定基ControlDesigner類中 屬性所傳ViewControl回的控件類型。 SupportsPreviewControlAttribute如果未在控制項設計工具宣告中指定 ,則ControlDesigner物件行為相當於將 屬性指定SupportsPreviewControlfalse

適用於

另請參閱