Using Web Parts in ASP.Net 2.0

Web Parts in ASP.Net 2.0 are used to customize the web applications. A web page is divided into different areas and each area could be a web part that can customized according to the users preference.

The person who is browsing that web page can customize that web page.
A Web part can have any standard ASP.Net control, user control, and custom control within it. You can also build your own custom control, which can be displayed in a web part. If you are creating your own custom control to be used in a web part, that control has to be derived from the WebPart Class. When you are using the WebPart class you have to import the System.Web.UI.WebControls.WebParts namespace. The WebParts class has a method called RenderContents, which is used to display the contents in a WebPart.

To include WebParts in a web page, you need to have one WebPartManager control in that page. Only one WebPartManager control is necessary to control all the WebParts in a particular page. The WebParts in a page can further be divided into WebPartZones each of which can display different contents. Optionally a WebZone can have CatalogZone control, which is used to display the catalogs of WebParts. The following code displays the WebPartManager control and a WebPartZone in a web page.

<head> </head>
<form id="Form1" runat="server">
<asp:WebPartManager ID="WebPartManager1" Runat="Server" />
<td valign="top" bgcolor="white" width="60%">
Runat="Server" />
<td valign="top" bgcolor="white" width="40%" >
<asp:CatalogZone ID="CatalogZone1" Runat="server">
ID="PageCatalogPart1" Runat="server" />
ID="DeclarativeCatalogPart1" Runat="server">
‘Web Parts Control you create comes here.


When the DisplayMode property of the WebPartManager is set to CatalogDisplayMode, you can add WebParts to the page. The CatalogZone control displays the Page Catalog and Declarative Catalog. The DeclarativeCatalogPart in the CatalogZone displays all the web controls to be displayed. These web controls are held in the WebPartsTemplate. The code listing given above gives a clear picture of the hierarchy of the web part controls in a web page.

Within the WebPartsTemplate, you can display the content that is needed for the parts. You can create your own web parts that are to be displayed. We will consider a case, where you need to display the list of employees in a particular department of an organization. This data needs to be displayed within a WebPart in a web page. To do this, you have to perform the following steps:

1. Import the System.Web.UI.WebControls.WebParts namespace.
2. Create a class that inherits the WebPart class (for example., let the class name be “DeptEmployees”)
3. Declare a connection string to connect to the database which has the departments and employee details.
4. Declare a query string to retrieve the required data from the database.
5. Override the RenderContents method of the WebPart class
6. Within the RenderContents method:
a. Declare a DataTable
b. Declare a DataAdapter
c. Fill the DataTable with the data using the DataAdapter, Connection String, and Query String.
d. Select a Row from the DataTable
e. Use the Write method of the HtmlTextWriter object to display the data.

Using the above steps, you can create a custom WebPart named DeptEmployees and you can display it using the following tag, within the CatalogZone.

<custom:DeptEmployees ID="myDeptEmp" Runat="Server" />

Once you have the web part in a page, you can add the web part from the declarative catalog to the web part zones using the ADD button in the declarative catalog zone. A web part thus added to a web part zone can be moved to any web part zone, by simply dragging the web part to that particular zone.

As you know that web parts are different areas of a web page that can be customized, you can also build more complex web parts that can be customized by the user who is using the web page. It is also possible to share information between different web parts. For example, you can create web parts so that when you select the department information from a drop down list in a web part, that data is transferred to another web part so that the other web part displays all the employees of the selected department. Thus connections between web parts can be static or dynamic. Static connections are created when you code the web parts and dynamic connections are created by the user at runtime. It is also possible to use the same web parts in different web applications and different web sites by exporting and importing the web parts.

“Amazon and the Amazon logo are trademarks of, Inc. or its affiliates.”

| Privacy Policy for | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.