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.

<html>
<head> </head>
<body>
<form id="Form1" runat="server">
<asp:WebPartManager ID="WebPartManager1" Runat="Server" />
<table>
<tr>
<td valign="top" bgcolor="white" width="60%">
<asp:WebPartZone
ID="WebPartZone1"
Width="100%"
HeaderStyle-BackColor="Blue"
PartTitleStyle-BackColor="Silver"
Runat="Server" />
</td>
<td valign="top" bgcolor="white" width="40%" >
<asp:CatalogZone ID="CatalogZone1" Runat="server">
<ZoneTemplate>
<asp:PageCatalogPart
ID="PageCatalogPart1" Runat="server" />
<asp:DeclarativeCatalogPart
ID="DeclarativeCatalogPart1" Runat="server">
<WebPartsTemplate>
‘Web Parts Control you create comes here.
</WebPartsTemplate>
</asp:DeclarativeCatalogPart>
</ZoneTemplate>
</asp:CatalogZone>
</td>

</tr>
</table>
</body>
</html>

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.


_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.