Читати англійською Редагувати

Поділитися через


ControlDesigner.Initialize(IComponent) Method

Definition

Initializes the control designer and loads the specified component.

C#
public override void Initialize(System.ComponentModel.IComponent component);

Parameters

component
IComponent

The control being designed.

Examples

The following code example demonstrates how to use a control class and a control designer class that override the Initialize method to initialize internal variables.

C#
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;

namespace AspNet.Samples
{
    // Create a custom class to render the Text property
    [Designer(typeof(SimpleDesigner)), DefaultProperty("Text"), 
    ToolboxData("<{0}:Simple runat=\"server\"></{0}:Simple>")]
    public sealed class Simple : WebControl
    {
        public Simple()
        { }

        // Create a Text property
        [Browsable(true), Bindable(true), 
            PersistenceMode(PersistenceMode.Attribute)]
        public string Text
        {
            get
            {
                object o = ViewState["TextProp"];
                return (o == null) ? "Sample Text" : (string)o;
            }
            set { ViewState["TextProp"] = value; }
        }

        // Render the text inside the control
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write(Text);
        }
    }
}

namespace AspNet.Samples
{
    //Create a designer class for the Simple control
    public sealed class SimpleDesigner : ControlDesigner
    {
        // Declare a reference to the Simple class
        private Simple simpleControl;

        public SimpleDesigner()
        { }

        public override void Initialize(IComponent ponent)
        {
            base.Initialize(ponent);

            // Get a reference to the control
            simpleControl = (Simple)ponent;

            //Set Text to the control's ID
            simpleControl.Text = simpleControl.ID;
        }

        // Allow resizing the control in the design host
        public override bool AllowResize
        {
            get
            {
                return true;
            }
        }

        public override string GetDesignTimeHtml()
        {
            if (simpleControl.Text.Length > 0)
            {
                string spec = "<a href='{0}.aspx'>{0}</a>";
                return String.Format(spec, simpleControl.Text);
            }
            else
            {
                return GetEmptyDesignTimeHtml();
            }
        }
    }
}

Remarks

The Initialize method is called by the design host to complete the following actions:

  • Load the control designer with the component to design.

  • Set up the view on the control using the SetViewFlags method.

  • Verify that the associated control is of the right type.

Applies to

Продукт Версії
.NET Framework 1.1, 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

See also