DeclarativeCatalogPart::GetWebPart Method
Returns a reference to a WebPart control based on the value of the description passed into the method.
Assembly: System.Web (in System.Web.dll)
Parameters
- description
- Type: System.Web.UI.WebControls.WebParts::WebPartDescription
A WebPartDescription that contains details about the control.
Return Value
Type: System.Web.UI.WebControls.WebParts::WebPartA WebPart control whose description matches the values in description.
| Exception | Condition |
|---|---|
| ArgumentNullException | Description is nullptr. |
| ArgumentException | Description is not an available WebPartDescription instance. |
The GetWebPart method returns a reference to a WebPart control whose description details match the values of the WebPartDescription object passed into the method. Typically, this method is used together with the GetAvailableWebPartDescriptions method, which is used to retrieve the descriptions of all the controls in the catalog. Individual WebPart controls can then be retrieved or manipulated as needed by passing individual WebPartDescription objects to the GetWebPart method.
The following code example shows how to use the GetWebPart method. This example has four parts:
A user control that enables you to change display modes on a Web Parts page.
A source code file that contains two custom WebPart controls.
A Web page that contains a CatalogZone control and a DeclarativeCatalogPart control that references several server controls. The page also contains the event-handling code that calls the GetWebPart method.
An explanation of how the example works when you load the page in a browser.
The first part of this code example is the user control that enables users to change display modes on a Web page. For details about display modes and a description of the source code in this control, see Walkthrough: Changing Display Modes on a Web Parts Page.
The second part of the code example is the source code for the two custom WebPart controls. For the code example to run, you must compile this source code. You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. For a walkthrough that demonstrates both methods of compiling, see Walkthrough: Developing and Using a Custom Web Server Control.
The third part of the example is the Web page that contains the DeclarativeCatalogPart control. Notice that the Button2_Click method obtains the descriptions of the WebPart controls in the catalog, calls the GetWebPart method to retrieve a control that matches the description of the third control in the collection, and then adds that control to the page.
When you load the page in a browser, select Catalog Mode in the Display Mode drop-down list control to switch to catalog mode. In catalog mode, you can see the controls that are available to be added to the page. If you click the Add TextDisplayWebPart button, the control is added to the page without your having to add it through the normal catalog user interface (UI). Use the Display Mode drop-down list to return the page to normal browse mode; the control you added is visible on the page.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.