WebPartManager.CopyWebPart(WebPart) Method

Definition

Used by the Web Parts control set to create a copy of a WebPart or server control for the purpose of adding the control to a Web page.

protected:
 virtual System::Web::UI::WebControls::WebParts::WebPart ^ CopyWebPart(System::Web::UI::WebControls::WebParts::WebPart ^ webPart);
protected virtual System.Web.UI.WebControls.WebParts.WebPart CopyWebPart (System.Web.UI.WebControls.WebParts.WebPart webPart);
abstract member CopyWebPart : System.Web.UI.WebControls.WebParts.WebPart -> System.Web.UI.WebControls.WebParts.WebPart
override this.CopyWebPart : System.Web.UI.WebControls.WebParts.WebPart -> System.Web.UI.WebControls.WebParts.WebPart
Protected Overridable Function CopyWebPart (webPart As WebPart) As WebPart

Parameters

webPart
WebPart

The WebPart or server control to be copied.

Returns

A WebPart to be added to a page.

Remarks

You cannot call the CopyWebPart method directly from your code. This method is called internally by the WebPartManager control as part of the process of adding a new dynamic WebPart or server control to a page. A dynamic control is added to a page programmatically or through the Web Parts user interface (UI), for example by a user adding a control from a catalog of controls, as opposed to a static control, which is declared directly in the markup of a page.

Note

The method can be overridden in a derived class if developers want to enable the method to handle additional control copying scenarios. For details, see the Notes to Inheritors section.

When a new dynamic control is added, if it is a WebPart control, the CopyWebPart method returns a new instance of the control. If the control being added is some other type of server control (such as a user control, a custom control, or an ASP.NET control), the control will already have been wrapped with a GenericWebPart object by the Web Parts control set. When the CopyWebPart method encounters a GenericWebPart control, it returns a new instance of the GenericWebPart control with a new instance of the child control wrapped within it.

When the CopyWebPart method creates a new copy of a control to return, it also resets the values of all the properties to their default values. Note that, if you want to preserve the values of the personalizable properties and have them copied to the new control instance, you should call the CopyPersonalizationState method as well. The final step carried out by the CopyWebPart method is to call the CreateDynamicWebPartID method to obtain a new ID for the control.

Note

Because the method obtains a new ID for a copied control, you should not rely on referencing a dynamic control that is added to a page by its original ID. Instead, you should reference the new instance of the control returned by the method.

Notes to Inheritors

The method is declared as virtual so that developers could inherit from the WebPartManager class, override the method, and provide for additional scenarios in which it could create copies of controls. For example, the method could optionally receive as input a control that has been serialized into an XML file. The method could deserialize the XML (if present), and then call the base method to handle the existing cases and to return a new instance of a WebPart control.

Applies to

See also