ImportCatalogPart::GetWebPart Method (WebPartDescription^)
Returns a reference to a WebPart control based on the values in 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::WebPart^A WebPart control whose description matches the values in description.
| Exception | Condition |
|---|---|
| ArgumentNullException | description is null. |
| ArgumentException | description is not an available WebPartDescription value. |
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 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 demonstrates how to use the GetWebPart method on a Web page. The example has four parts:
A user control that enables you to change display modes on a Web Parts page.
A Web page that contains a CatalogZone control and an ImportCatalogPart control, along with code that uses the GetWebPart method.
A source code file that contains two custom WebPart controls.
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. You should place the following source code in a file and name it Displaymodemenucs.ascx or Displaymodemenuvb.ascx (depending on which language you are using). 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 Web page. At the top of the page are two register directives, one for the user control and one for the compiled component that contains the two custom WebPart controls. Both of these controls are referenced declaratively in the markup of the page. On the declarative references to the WebPart controls (both begin with an aspSample prefix), note that each has an exportMode="all" attribute added to it. This attribute enables you to export a .WebPart description file for the control, which you will use in a later to step to import the control to a page.
Note |
|---|
To enable users of a Web Parts application to export a description file for WebPart controls, you must also enable the export feature in the Web application by adding an enableExport="true" attribute to the <webParts> element (which is a child of the <system.web> element) in the Web.config file. Export is disabled by default, so if you have not yet enabled export for your application, edit the Web.config file and do it now. |
The Web page also has a declarative reference to the ImportCatalogPart control, nested within the proper hierarchy of declarative elements. The GetWebPart method is called within the code for the Button2_Click method.
The third part of the code example is the source code for the two 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. This code example uses dynamic compilation. For a walkthrough that demonstrates both methods of compiling, see Walkthrough: Developing and Using a Custom Web Server Control.
Now run the code example. Load the Web page in a browser. The first step is export a .WebPart description file for each WebPart control (for the TextDisplayWebPart and for the UserInfoWebPart control). For each control, click the verbs menu on the control (represented by the downward arrow in the title bar), and click Export. Follow the instructions to save a .WebPart description file for the control. After you have exported a description file for each control, close the Web page, and edit the page source in an editor. Delete the <aspSample:userinfowebpart> and the <aspSample:textdisplaywebpart> control declaration elements, then save and close the file. (You are doing this step to simulate a user who does not yet have these controls, so you can import the controls to the page).
Load the Web page again in a browser. Use the Display Mode drop-down list control and select Catalog to switch the page to catalog mode. In the ImportCatalogPart control, click the Browse button, browse to the .WebPart files you created, select one, then click the Upload button. A reference to the control should appear with a check box next to it. Now that the control description is uploaded to the ImportCatalogPart control, click the Use GetWebPart button near the bottom of the page. This will demonstrate the effect of calling the GetWebPart method and passing to it the control description currently loaded in the ImportCatalogPart control. Note that the associated server control is added immediately to the Web page, without the user having to click the Add button. The GetWebPart method, which is called in the Button2_Click method of the page source, returns the WebPart control associated with the current description that is loaded in the ImportCatalogPart control. Next the AddWebPart method is called, and the WebPart control is directly added to the page. This demonstrates how to add a control programmatically from the ImportCatalogPart control without user intervention.
After adding the first control, repeat the process to add the second control to the page. Finally, click Close to exit catalog mode and return the page to browse mode. Both custom controls should now appear in the page, containing the values they had when you exported description files earlier.
Available since 2.0
