次の方法で共有


ControlPersister クラス

デザイン時に Web サーバー コントロールを永続化するメソッドを提供します。

この型のすべてのメンバの一覧については、ControlPersister メンバ を参照してください。

System.Object
   System.Web.UI.Design.ControlPersister

NotInheritable Public Class ControlPersister
[C#]
public sealed class ControlPersister
[C++]
public __gc __sealed class ControlPersister
[JScript]
public class ControlPersister

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

メモ   Web フォーム デザイナは、Windows フォーム コントロールがプロパティの永続化のためにオプションで公開する ShouldSerializePropertyName メソッドおよび ResetPropertyName メソッドをサポートしていません。サーバー コントロールは、 ControlPersister クラスによって提供されるメソッドを使用して、シリアル化されます。これらのメソッドの実装は DefaultValueAttributePersistenceModeAttributeDesignerSerializationVisibilityAttribute などのメタデータ属性によって実行されます。

使用例

 
Imports System
Imports System.Drawing
Imports System.Drawing.Design
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

' Web control designer provides an interface 
' to use the ControlPersister and ControlParser.
Public Class ControlParserPersisterDesigner
    Inherits System.Web.UI.Design.ControlDesigner

    Public Sub New()
    End Sub

    ' Provides designer verb menu commands to 
    ' persist a control and to load a persisted control.
    Public Overrides ReadOnly Property Verbs() _
    As System.ComponentModel.Design.DesignerVerbCollection
        Get
            Dim dvc As New DesignerVerbCollection()
            dvc.Add(New DesignerVerb("Load Persisted Control...", _
                New EventHandler(AddressOf Me.loadPersistedControl)))
            dvc.Add(New DesignerVerb("Parse and Save Control...", _
                New EventHandler(AddressOf Me.saveControl)))
            Return dvc
        End Get
    End Property

    ' Displays a textbox form to receive an HTML 
    ' string that represents a control, and creates
    ' a toolbox item for the control, if not already
    ' present in the selected toolbox category.
    Private Sub loadPersistedControl(ByVal sender As Object, ByVal e As EventArgs)
        ' Display a StringInputForm to obtain a persisted control string.
        Dim inputForm As New StringInputForm()

        If inputForm.ShowDialog() <> DialogResult.OK Then
            Return
        End If
        If inputForm.tbox.Text.Length < 2 Then
            Return
        End If
        ' Obtain an IDesignerHost for the design-mode document.
        Dim host As IDesignerHost = CType(Me.Component.Site.GetService(GetType(IDesignerHost)), IDesignerHost)

        ' Create a Web control from the persisted control string.
        Dim ctrl As System.Web.UI.Control = ControlParser.ParseControl(host, inputForm.tbox.Text.Trim())

        ' Create a Web control toolbox item for the type of the control.
        Dim item As New System.Web.UI.Design.WebControlToolboxItem(ctrl.GetType())

        ' Add the Web control toolbox item to the toolbox.
        Dim toolService As IToolboxService = CType(Me.Component.Site.GetService(GetType(IToolboxService)), IToolboxService)
        If Not (toolService Is Nothing) Then
            toolService.AddToolboxItem(item)
        Else
            Throw New Exception("IToolboxService was not found.")
        End If
    End Sub

    ' Displays a list of controls in the project, if any,
    ' and displays the HTML representing the persisted, selected control.       
    Private Sub saveControl(ByVal sender As Object, ByVal e As EventArgs)
        ' Get the collection of components in the current 
        ' design mode document.
        Dim documentComponents As ComponentCollection = _
            Me.Component.Site.Container.Components

        ' Filter the components to those that derive from the 
        ' System.Web.UI.Control class.

        ' Create an IComponent array of the maximum possible length needed.
        Dim filterArray(documentComponents.Count) As IComponent

        ' Count the total qualifying components.
        Dim total As Integer = 0
        Dim i As Integer
        For i = 0 To documentComponents.Count - 1
            ' If the component derives from System.Web.UI.Control, 
            ' copy a reference to the control to the filterArray 
            ' array and increment the control count.
            If GetType(System.Web.UI.Control).IsAssignableFrom(CType(documentComponents(i), Object).GetType()) Then
                If Not (CType(documentComponents(i), System.Web.UI.Control).UniqueID Is Nothing) Then
                    filterArray(total) = documentComponents(i)
                    total += 1
                End If
            End If
        Next i

        If total = 0 Then
            Throw New Exception( _
                "Document contains no System.Web.UI.Control components.")
        End If

        ' Move the components that derive from System.Web.UI.Control to a 
        ' new array of the correct size.
        Dim controlArray(total - 1) As System.Web.UI.Control

        For i = 0 To total - 1
            controlArray(i) = CType(filterArray(i), System.Web.UI.Control)
        Next i

        ' Create a ControlSelectionForm to provide a persistence 
        ' configuration interface.
        Dim selectionForm As New ControlSelectionForm(controlArray)

        ' Display the form.
        If selectionForm.ShowDialog() <> DialogResult.OK Then
            Return
        End If

        ' Validate the selection.
        If selectionForm.controlList.SelectedIndex = -1 Then
            Throw New Exception("You must select a control to persist.")
        End If

        ' Parse the selected control.
        Dim persistedData As String = ControlPersister.PersistControl( _
            controlArray(selectionForm.controlList.SelectedIndex))

        ' Display the control persistence string in a text box.
        Dim displayForm As New StringDisplayForm(persistedData)
        displayForm.ShowDialog()
    End Sub

End Class

' Simple text display control hosts the ControlParserPersisterDesigner.
<DesignerAttribute(GetType(ControlParserPersisterDesigner), _
    GetType(IDesigner))> _
Public Class ControlParserPersisterDesignerControl
    Inherits System.Web.UI.WebControls.WebControl

    Private [text_] As String

    <Bindable(True), Category("Appearance"), DefaultValue("")> _
    Public Property [Text]() As String
        Get
            Return [text_]
        End Get
        Set(ByVal Value As String)
            [text_] = Value
        End Set
    End Property

    Public Sub New()
        [Text] = "Right-click here to access control persistence " & _
                    "methods in design mode"
    End Sub

    Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
        output.Write([Text])
    End Sub
End Class

' Provides a form with a list for selection.
Friend Class ControlSelectionForm
    Inherits System.Windows.Forms.Form
    Private controlArray() As System.Web.UI.Control
    Public controlList As System.Windows.Forms.ListBox

    Public Sub New(ByVal controlArray() As System.Web.UI.Control)
        Me.controlArray = controlArray

        Me.Size = New Size(400, 500)
        Me.Text = "Control Persister Selection Form"

        Me.SuspendLayout()

        Dim label1 As New System.Windows.Forms.Label()
        label1.Text = "Select the control to persist:"
        label1.Size = New Size(240, 20)
        label1.Location = New Point(10, 10)
        Me.Controls.Add(label1)

        controlList = New System.Windows.Forms.ListBox()
        controlList.Size = New Size(370, 400)
        controlList.Location = New Point(10, 30)
        controlList.Anchor = AnchorStyles.Left Or AnchorStyles.Top _
                Or AnchorStyles.Bottom Or AnchorStyles.Right
        Me.Controls.Add(controlList)

        Dim okButton As New System.Windows.Forms.Button()
        okButton.Text = "OK"
        okButton.Size = New Size(80, 24)
        okButton.Location = New Point(Me.Width - 100, Me.Height - 60)
        okButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
        okButton.DialogResult = DialogResult.OK
        Me.Controls.Add(okButton)

        Dim cancelButton As New System.Windows.Forms.Button()
        cancelButton.Text = "Cancel"
        cancelButton.Size = New Size(80, 24)
        cancelButton.Location = New Point(Me.Width - 200, Me.Height - 60)
        cancelButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
        cancelButton.DialogResult = DialogResult.Cancel
        Me.Controls.Add(cancelButton)

        Dim i As Integer
        For i = 0 To controlArray.Length - 1
            controlList.Items.Add(controlArray(i).UniqueID)
        Next i
        Me.ResumeLayout()
    End Sub
End Class

' Provides a form with a multiline text box display.
Friend Class StringDisplayForm
    Inherits System.Windows.Forms.Form

    Public Sub New(ByVal displayText As String)
        Me.Size = New Size(400, 400)
        Me.Text = "Control Persistence String"

        Me.SuspendLayout()
        Dim tbox As New System.Windows.Forms.TextBox()
        tbox.Multiline = True
        tbox.Size = New Size(Me.Width - 40, Me.Height - 90)
        tbox.Text = displayText
        Me.Controls.Add(tbox)

        Dim okButton As New System.Windows.Forms.Button()
        okButton.Text = "OK"
        okButton.Size = New Size(80, 24)
        okButton.Location = New Point(Me.Width - 100, Me.Height - 60)
        okButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
        okButton.DialogResult = DialogResult.OK
        Me.Controls.Add(okButton)

        Me.ResumeLayout()
    End Sub
End Class

' Provides a form with a multiline text box for input.
Friend Class StringInputForm
    Inherits System.Windows.Forms.Form
    Public tbox As System.Windows.Forms.TextBox

    Public Sub New()
        Me.Size = New Size(400, 400)
        Me.Text = "Input Control Persistence String"

        Me.SuspendLayout()
        tbox = New System.Windows.Forms.TextBox()
        tbox.Multiline = True
        tbox.Size = New Size(Me.Width - 40, Me.Height - 90)
        tbox.Text = ""
        Me.Controls.Add(tbox)

        Dim okButton As New System.Windows.Forms.Button()
        okButton.Text = "OK"
        okButton.Size = New Size(80, 24)
        okButton.Location = New Point(Me.Width - 100, Me.Height - 60)
        okButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
        okButton.DialogResult = DialogResult.OK
        Me.Controls.Add(okButton)

        Dim cancelButton As New System.Windows.Forms.Button()
        cancelButton.Text = "Cancel"
        cancelButton.Size = New Size(80, 24)
        cancelButton.Location = New Point(Me.Width - 200, Me.Height - 60)
        cancelButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
        cancelButton.DialogResult = DialogResult.Cancel
        Me.Controls.Add(cancelButton)

        Me.ResumeLayout()
    End Sub
End Class

[C#] 
using System;
using System.Drawing;
using System.Drawing.Design;
using System.IO;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace ControlParserPersisterDesignerControl
{
    // Web control designer provides an interface 
    // to use the ControlPersister and ControlParser.
    public class ControlParserPersisterDesigner : 
        System.Web.UI.Design.ControlDesigner
    {
        public ControlParserPersisterDesigner() : base()
        {
        }

        // Provides designer verb menu commands to 
        // persist a control and to load a persisted control.
        public override 
            System.ComponentModel.Design.DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection dvc = new DesignerVerbCollection();                               
                dvc.Add( new DesignerVerb("Load Persisted Control...", 
                    new EventHandler(this.loadPersistedControl)) ); 
                dvc.Add( new DesignerVerb("Parse and Save Control...", 
                    new EventHandler(this.saveControl)) ); 
                return dvc;
            }
        }

        // Displays a textbox form to receive an HTML 
        // string that represents a control, and creates
        // a toolbox item for the control, if not already
        // present in the selected toolbox category.
        private void loadPersistedControl(object sender, EventArgs e)
        {
            // Display a StringInputForm to obtain a persisted control string.
            StringInputForm inputForm = new StringInputForm();

            if( inputForm.ShowDialog() != DialogResult.OK )
                return;
            if( inputForm.tbox.Text.Length < 2 )
                return;

            // Obtain an IDesignerHost for the design-mode document.
            IDesignerHost host = (IDesignerHost)
                this.Component.Site.GetService(typeof(IDesignerHost));

            // Create a Web control from the persisted control string.
            System.Web.UI.Control ctrl = 
                ControlParser.ParseControl(host, inputForm.tbox.Text.Trim());

            // Create a Web control toolbox item for the type of the control
            System.Web.UI.Design.WebControlToolboxItem item = 
                new System.Web.UI.Design.WebControlToolboxItem(ctrl.GetType());
            
            // Add the Web control toolbox item to the toolbox
            IToolboxService toolService = (IToolboxService)
                this.Component.Site.GetService(typeof(IToolboxService));
            if( toolService != null )
                toolService.AddToolboxItem(item);
            else
                throw new Exception("IToolboxService was not found.");                               
        }

        // Displays a list of controls in the project, if any,
        // and displays the HTML representing the persisted, selected control.       
        private void saveControl(object sender, EventArgs e)
        {
            // Get the collection of components in the current 
            // design mode document.
            ComponentCollection documentComponents = 
                this.Component.Site.Container.Components;
                        
            // Filter the components to those that derive from the 
            // System.Web.UI.Control class.

            // Create an IComponent array of the maximum possible length needed.
            IComponent[] filterArray = new IComponent[documentComponents.Count];
            
            // Count the total qualifying components.
            int total = 0;
            for(int i=0; i<documentComponents.Count; i++)
            {
                // If the component derives from System.Web.UI.Control, 
                // copy a reference to the control to the filterArray 
                // array and increment the control count.
                if( typeof(System.Web.UI.Control).IsAssignableFrom( 
                    documentComponents[i].GetType() ) )
                {
                    if( ((System.Web.UI.Control)documentComponents[i]).UniqueID 
                        != null )
                    {
                        filterArray[total] = documentComponents[i];
                        total++;
                    }
                }
            }

            if( total == 0 )
                throw new Exception(
                    "Document contains no System.Web.UI.Control components.");

            // Move the components that derive from System.Web.UI.Control to a 
            // new array of the correct size.
            System.Web.UI.Control[] controlArray = 
                new System.Web.UI.Control[total];
            for(int i=0; i<total; i++)
                controlArray[i] = (System.Web.UI.Control)filterArray[i];              

            // Create a ControlSelectionForm to provide a persistence 
            // configuration interface.
            ControlSelectionForm selectionForm = 
                new ControlSelectionForm(controlArray);

            // Display the form.
            if( selectionForm.ShowDialog() != DialogResult.OK )
                return;
            
            // Validate the selection.
            if( selectionForm.controlList.SelectedIndex == -1 )
                throw new Exception("You must select a control to persist.");

            // Parse the selected control.
            string persistedData = ControlPersister.PersistControl(
                controlArray[selectionForm.controlList.SelectedIndex]);

            // Display the control persistence string in a text box.
            StringDisplayForm displayForm = 
                new StringDisplayForm(persistedData);
            displayForm.ShowDialog();
        }
    }

    // Simple text display control hosts the ControlParserPersisterDesigner.
    [DesignerAttribute(typeof(ControlParserPersisterDesigner), 
         typeof(IDesigner))]
    public class ControlParserPersisterDesignerControl : 
        System.Web.UI.WebControls.WebControl
    {        
        private string text;

        [Bindable(true),
        Category("Appearance"),
        DefaultValue("")]
        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }

        public ControlParserPersisterDesignerControl() : base()
        {
            text = "Right-click here to access control persistence " +
                "methods in design mode";            
        }

        protected override void Render(HtmlTextWriter output)
        {
            output.Write(Text);
        }        
    }

    // Provides a form with a list for selection.
    internal class ControlSelectionForm : System.Windows.Forms.Form
    {
        private System.Web.UI.Control[] controlArray;
        public System.Windows.Forms.ListBox controlList;

        public ControlSelectionForm(System.Web.UI.Control[] controlArray)
        {
            this.controlArray = controlArray;
            
            this.Size = new Size( 400, 500 );            
            this.Text = "Control Persister Selection Form";

            this.SuspendLayout();

            System.Windows.Forms.Label label1 = 
                new System.Windows.Forms.Label();
            label1.Text = "Select the control to persist:";
            label1.Size = new Size( 240, 20 );
            label1.Location = new Point(10, 10 );
            this.Controls.Add( label1 );

            controlList = new System.Windows.Forms.ListBox();        
            controlList.Size = new Size( 370, 400 );
            controlList.Location = new Point( 10, 30 );
            controlList.Anchor = AnchorStyles.Left | AnchorStyles.Top | 
                AnchorStyles.Bottom | AnchorStyles.Right;
            this.Controls.Add( controlList );

            System.Windows.Forms.Button okButton = 
                new System.Windows.Forms.Button();
            okButton.Text = "OK";
            okButton.Size = new Size(80, 24);
            okButton.Location = 
                new Point( this.Width - 100, this.Height - 60 );
            okButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            okButton.DialogResult = DialogResult.OK;
            this.Controls.Add( okButton );

            System.Windows.Forms.Button cancelButton = 
                new System.Windows.Forms.Button();
            cancelButton.Text = "Cancel";
            cancelButton.Size = new Size(80, 24);
            cancelButton.Location = 
                new Point( this.Width - 200, this.Height - 60 );
            cancelButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            cancelButton.DialogResult = DialogResult.Cancel;
            this.Controls.Add( cancelButton );

            for( int i=0; i< controlArray.Length; i++ )
                controlList.Items.Add( controlArray[i].UniqueID );

            this.ResumeLayout();
        }
    }

    // Provides a form with a multiline text box display.
    internal class StringDisplayForm : System.Windows.Forms.Form
    {
        public StringDisplayForm(string displayText)
        {
            this.Size = new Size(400, 400);
            this.Text = "Control Persistence String";
            
            this.SuspendLayout();
            System.Windows.Forms.TextBox tbox = 
                new System.Windows.Forms.TextBox();
            tbox.Multiline = true;
            tbox.Size = new Size(this.Width-40, this.Height-90);            
            tbox.Text = displayText;
            this.Controls.Add(tbox);

            System.Windows.Forms.Button okButton = 
                new System.Windows.Forms.Button();
            okButton.Text = "OK";
            okButton.Size = new Size(80, 24);
            okButton.Location = 
                new Point( this.Width - 100, this.Height - 60 );
            okButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            okButton.DialogResult = DialogResult.OK;
            this.Controls.Add( okButton );

            this.ResumeLayout();
        }
    }

    // Provides a form with a multiline text box for input.
    internal class StringInputForm : System.Windows.Forms.Form
    {
        public System.Windows.Forms.TextBox tbox;

        public StringInputForm()
        {
            this.Size = new Size(400, 400);
            this.Text = "Input Control Persistence String";
            
            this.SuspendLayout();
            tbox = new System.Windows.Forms.TextBox();
            tbox.Multiline = true;
            tbox.Size = new Size(this.Width-40, this.Height-90);            
            tbox.Text = "";
            this.Controls.Add(tbox);

            System.Windows.Forms.Button okButton = 
                new System.Windows.Forms.Button();
            okButton.Text = "OK";
            okButton.Size = new Size(80, 24);
            okButton.Location = 
                new Point( this.Width - 100, this.Height - 60 );
            okButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            okButton.DialogResult = DialogResult.OK;
            this.Controls.Add( okButton );

            System.Windows.Forms.Button cancelButton = 
                new System.Windows.Forms.Button();
            cancelButton.Text = "Cancel";
            cancelButton.Size = new Size(80, 24);
            cancelButton.Location = 
                new Point( this.Width - 200, this.Height - 60 );
            cancelButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            cancelButton.DialogResult = DialogResult.Cancel;
            this.Controls.Add( cancelButton );

            this.ResumeLayout();
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Web.dll>
#using <System.Design.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Drawing::Design;
using namespace System::IO;
using namespace System::Web::UI;
using namespace System::Web::UI::Design;
using namespace System::Web::UI::WebControls;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::Design;

// Provides a form with a multiline text box display.
private __gc class StringDisplayForm : 
   public System::Windows::Forms::Form {
public:
   StringDisplayForm(String* displayText) {
      this->Size = System::Drawing::Size(400, 400);
      this->Text = S"Control Persistence String";

      this->SuspendLayout();
      System::Windows::Forms::TextBox* tbox = 
         new System::Windows::Forms::TextBox();
      tbox->Multiline = true;
      tbox->Size = System::Drawing::Size(this->Width-40, this->Height-90);
      tbox->Text = displayText;
      this->Controls->Add(tbox);

      System::Windows::Forms::Button* okButton = 
         new System::Windows::Forms::Button();
      okButton->Text = S"OK";
      okButton->Size = System::Drawing::Size(80, 24);
      okButton->Location =  Point(this->Width - 100, this->Height - 60);
      okButton->Anchor = AnchorStyles(AnchorStyles::Bottom | AnchorStyles::Right);
      okButton->DialogResult = DialogResult::OK;
      this->Controls->Add(okButton);
      this->ResumeLayout();
   }
};

// Provides a form with a list for selection.
private __gc class ControlSelectionForm : 
   public System::Windows::Forms::Form {
private:
   System::Web::UI::Control*  controlArray[];
public:
   System::Windows::Forms::ListBox*  controlList;

public:
   ControlSelectionForm(System::Web::UI::Control* controlArray[]) {
      this->controlArray = controlArray;
      this->Size = System::Drawing::Size(400, 500);
      this->Text = S"Control Persister Selection Form";

      this->SuspendLayout();

      System::Windows::Forms::Label* label1 = 
         new System::Windows::Forms::Label();
      label1->Text = S"Select the control to persist:";
      label1->Size = System::Drawing::Size(240, 20);
      label1->Location =  Point(10, 10);
      this->Controls->Add(label1);
      controlList = new System::Windows::Forms::ListBox();
      controlList->Size = System::Drawing::Size(370, 400);
      controlList->Location =  Point(10, 30);
      controlList->Anchor = AnchorStyles(AnchorStyles::Left | AnchorStyles::Top |
         AnchorStyles::Bottom | AnchorStyles::Right);
      this->Controls->Add(controlList);

      System::Windows::Forms::Button* okButton = 
         new System::Windows::Forms::Button();
      okButton->Text = S"OK";
      okButton->Size = System::Drawing::Size(80, 24);
      okButton->Location =  Point(this->Width - 100, this->Height - 60);
      okButton->Anchor = AnchorStyles(AnchorStyles::Bottom | AnchorStyles::Right);
      okButton->DialogResult = DialogResult::OK;
      this->Controls->Add(okButton);

      System::Windows::Forms::Button* cancelButton = 
         new System::Windows::Forms::Button();
      cancelButton->Text = S"Cancel";
      cancelButton->Size = System::Drawing::Size(80, 24);
      cancelButton->Location =  Point(this->Width - 200, this->Height - 60);
      cancelButton->Anchor = AnchorStyles(AnchorStyles::Bottom | AnchorStyles::Right);
      cancelButton->DialogResult = DialogResult::Cancel;
      this->Controls->Add(cancelButton);

      for (int i = 0; i < controlArray->Length; i++)
         controlList->Items->Add(controlArray[i]->UniqueID);
      this->ResumeLayout();
   }
};

// Provides a form with a multiline text box for input.
private __gc class StringInputForm : public Form {
public:
   System::Windows::Forms::TextBox*  tbox;

public:
   StringInputForm() {
      this->Size = System::Drawing::Size(400, 400);
      this->Text = S"Input Control Persistence String";

      this->SuspendLayout();
      tbox = new System::Windows::Forms::TextBox();
      tbox->Multiline = true;
      tbox->Size = System::Drawing::Size(this->Width-40, this->Height-90);
      tbox->Text = S"";
      this->Controls->Add(tbox);

      System::Windows::Forms::Button* okButton = 
         new System::Windows::Forms::Button();
      okButton->Text = S"OK";
      okButton->Size = System::Drawing::Size(80, 24);
      okButton->Location =  Point(this->Width - 100, this->Height - 60);
      okButton->Anchor = AnchorStyles(AnchorStyles::Bottom | AnchorStyles::Right);
      okButton->DialogResult = DialogResult::OK;
      this->Controls->Add(okButton);

      System::Windows::Forms::Button* cancelButton = 
         new System::Windows::Forms::Button();
      cancelButton->Text = S"Cancel";
      cancelButton->Size = System::Drawing::Size(80, 24);
      cancelButton->Location =  Point(this->Width - 200, this->Height - 60);
      cancelButton->Anchor = 
         AnchorStyles(AnchorStyles::Bottom | AnchorStyles::Right);
      cancelButton->DialogResult = DialogResult::Cancel;
      this->Controls->Add(cancelButton);

      this->ResumeLayout();
   }
};

// Web control designer provides an interface
// to use the ControlPersister and ControlParser.
public __gc class ControlParserPersisterDesigner : 
   public System::Web::UI::Design::ControlDesigner {
public:
   ControlParserPersisterDesigner() : System::Web::UI::Design::ControlDesigner() {
   }

   // Provides designer verb menu commands to
   // persist a control and to load a persisted control.
public:

   __property System::ComponentModel::Design::DesignerVerbCollection* get_Verbs() {
      DesignerVerbCollection* dvc = new DesignerVerbCollection();
      dvc->Add(new DesignerVerb(S"Load Persisted Control...",
         new EventHandler(this,&ControlParserPersisterDesigner::loadPersistedControl)));
      dvc->Add(new DesignerVerb(S"Parse and Save Control...",
         new EventHandler(this,&ControlParserPersisterDesigner::saveControl)));
      return dvc;
   }

   // Displays a textbox form to receive an HTML
   // String* that represents a control, and creates
   // a toolbox item for the control, if not already
   // present in the selected toolbox category.
private:
   void loadPersistedControl(Object* /*sender*/, EventArgs* /*e*/) {
      // Display a StringInputForm to obtain a persisted control String*.
      StringInputForm* inputForm = new StringInputForm();

      if (inputForm->ShowDialog() != DialogResult::OK)
         return;
      if (inputForm->tbox->Text->Length < 2)
         return;

      // Obtain an IDesignerHost* for the design-mode document.
      IDesignerHost* host = dynamic_cast<IDesignerHost*>
         (this->Component->Site->GetService(__typeof(IDesignerHost)));

      // Create a Web control from the persisted control String*.
      System::Web::UI::Control* ctrl =
         ControlParser::ParseControl(host, inputForm->tbox->Text->Trim());

      // Create a Web control toolbox item for the type of the control
      WebControlToolboxItem* item = new WebControlToolboxItem(ctrl->GetType());

      // Add the Web control toolbox item to the toolbox
      IToolboxService* toolService = dynamic_cast<IToolboxService*>
         (this->Component->Site->GetService(__typeof(IToolboxService)));
      if (toolService != 0)
         toolService->AddToolboxItem(item);
      else
         throw new Exception(S"IToolboxService* was not found.");
   }

   // Displays a list of controls in the project, if any,
   // and displays the HTML representing the persisted, selected control.
private:
   void saveControl(Object* /*sender*/, EventArgs* /*e*/) {
      // Get the collection of components in the current
      // design mode document.
      ComponentCollection* documentComponents =
         this->Component->Site->Container->Components;

      // Filter the components to those that derive from the
      // System::Web::UI::Control class.

      // Create an IComponent* array of the maximum possible length needed.
      IComponent* filterArray [] = new IComponent*[documentComponents->Count];

      // Count the total qualifying components.
      int total = 0;
      for (int i=0; i<documentComponents->Count; i++) {
         // If the component derives from System::Web::UI::Control,
         // copy a reference to the control to the filterArray
         // array and increment the control count.
         if (__typeof(System::Web::UI::Control)->IsAssignableFrom(documentComponents->Item[i]->GetType())) {
            if ((dynamic_cast<System::Web::UI::Control*>(documentComponents->Item[i]))->UniqueID
               != 0) {
                  filterArray->Item[total] = documentComponents->Item[i];
                  total++;
               }
         }
      }

      if (total == 0)
         throw new Exception(S"Document contains no System::Web::UI::Control components.");

      // Move the components that derive from System::Web::UI::Control to a
      // new array of the correct size.
      System::Web::UI::Control* controlArray[] = 
         new System::Web::UI::Control*[total];
      for (int i=0; i<total; i++)
         controlArray->Item[i] = 
         dynamic_cast<System::Web::UI::Control*>(filterArray->Item[i]);

      // Create a ControlSelectionForm to provide a persistence
      // configuration interface.
      ControlSelectionForm* selectionForm = new ControlSelectionForm(controlArray);

      // Display the form.
      if (selectionForm->ShowDialog() != DialogResult::OK)
         return;

      // Validate the selection.
      if (selectionForm->controlList->SelectedIndex == -1)
         throw new Exception(S"You must select a control to persist.");

      // Parse the selected control.
      String* persistedData = 
         ControlPersister::PersistControl( controlArray[selectionForm->controlList->SelectedIndex]);

      // Display the control persistence String* in a text box.
      StringDisplayForm* displayForm = new StringDisplayForm(persistedData);
      displayForm->ShowDialog();
   }
};

// Simple text display control hosts the ControlParserPersisterDesigner.
[DesignerAttribute(__typeof(ControlParserPersisterDesigner), __typeof(IDesigner))]
public __gc class ControlParserPersisterDesignerControl : public WebControl {
private:
   String*  text;
public:
   [Bindable(true),
      Category(S"Appearance"),
      DefaultValue(S"")]
   __property String* get_Text() {
      return text;
   }
   __property void set_Text(String* value) {
      text = value;
   }

public:
   ControlParserPersisterDesignerControl() : WebControl() {
      text = S"Right-click here to access control persistence methods in design mode";
   }

protected:
   void Render(HtmlTextWriter* output) {
      output->Write(Text);
   }
};

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Web.UI.Design

プラットフォーム: Windows 2000, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Design (System.Design.dll 内)

参照

ControlPersister メンバ | System.Web.UI.Design 名前空間