SupportsPreviewControlAttribute Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Indicates whether a control designer requires a preview instance of the control at design time. This class cannot be inherited.
public ref class SupportsPreviewControlAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class)]
public sealed class SupportsPreviewControlAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class)>]
type SupportsPreviewControlAttribute = class
inherit Attribute
Public NotInheritable Class SupportsPreviewControlAttribute
Inherits Attribute
- Inheritance
- Attributes
The following code example demonstrates how to mark a control designer with the SupportsPreviewControlAttribute attribute. The code example derives an ASP.NET server control from the Label class and associates the ASP.NET server 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 encloses the design-time HTML for the control in italic tags.
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
Apply the SupportsPreviewControlAttribute attribute to a control designer class to indicate the type of preview control that is supported by the control designer. Use this attribute to change a preview control for design-time rendering without affecting the actual persisted instance of the associated control.
Typically, you specify the SupportsPreviewControlAttribute when declaring a custom designer class that is derived from the ControlDesigner class. The value of the SupportsPreviewControl property for the SupportsPreviewControlAttribute attribute determines the behavior for the UsePreviewControl and ViewControl members in the base ControlDesigner class.
Set the SupportsPreviewControl property to true
to indicate that the designer uses a temporary copy of the associated control to generate the design-time HTML. Changes to the temporary control are not persisted.
Set the SupportsPreviewControl property to false
to indicate that the designer returns the control instance, specifically the Component property, from the ViewControl method. Changes to the control object are persisted.
For example, the CalendarDesigner class is marked with the SupportsPreviewControlAttribute set to true
. The designer uses the preview control with the automatic style formatting task, which allows the user to preview various autoformat stylesthat can be applied to the calendar. As the user selects different autoformat styles in the user interface, the selected style scheme is applied to the preview control. Applying a new style to the preview control does not change the scheme that is applied to the instance of the Calendar control in the designer.
If the SupportsPreviewControlAttribute is not specified in the control designer declaration, the ControlDesigner behavior is equivalent to specifying the SupportsPreviewControl as false
.
Nota
Designer classes derived from the ControlDesigner class can override the UsePreviewControl and ViewControl members, and ignore the SupportsPreviewControlAttribute attribute. To determine the expected behavior for ViewControl and UsePreviewControl, see the reference documentation for the derived control designer class.
For general information about using attributes, see Attributes Overview and Attributes. For more information about design-time attributes, see Attributes and Design-Time Support.
Supports |
Initializes a new instance of the SupportsPreviewControlAttribute class and sets the initial value of the SupportsPreviewControl property. |
Default |
Gets an instance of the SupportsPreviewControlAttribute class that is set to the default preview value. This field is read-only. |
Supports |
Gets a value indicating whether the control designer requires a temporary preview control at design time. |
Type |
When implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute) |
Equals(Object) |
Determines whether the specified object represents the same preview attribute setting as the current instance of the SupportsPreviewControlAttribute class. |
Get |
Returns the hash code for this instance of the SupportsPreviewControlAttribute class. |
Get |
Gets the Type of the current instance. (Inherited from Object) |
Is |
Indicates whether the current instance of the SupportsPreviewControlAttribute class is set to the default preview attribute value. |
Match(Object) |
When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute) |
Memberwise |
Creates a shallow copy of the current Object. (Inherited from Object) |
To |
Returns a string that represents the current object. (Inherited from Object) |
_Attribute. |
Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute) |
_Attribute. |
Retrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute) |
_Attribute. |
Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute) |
_Attribute. |
Provides access to properties and methods exposed by an object. (Inherited from Attribute) |
Prodotto | Versioni |
---|---|
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Feedback su .NET
.NET è un progetto di open source. Selezionare un collegamento per fornire feedback: