Web Parts in ASP.NET 2.0
by Jesse Liberty01/10/2005
In a previous article, I discussed how personalization works. This article will pick up from where that one left off and show you how to use Web Parts to allow your users to further personalize their interactions with your web site.
Getting Started
There have been some small changes since the previous articles were published, and so I suggest you download the updated starter code. Unzip the starter project to any directory you like. It will create a subdirectory called Web Parts. Right click on the subdirectory and choose Sharing and Security. Click on the Web Sharing tab and choose Share this folder. Accept the defaults as offered, as shown in Figure 1.

Figure 1. Sharing the Web Parts directory
Start Visual Studio 2005 Beta 1 and choose File -> Open -> Open Web site. Navigate to the Web Parts directory and click Open. Click Debug (and save the .sln file) and you should be brought to the default page. Log in with the username Jesse (the starter kit includes this user) with the password Jesse. You're ready to begin to add Web Parts to what had been the Personalization project.
Begin by adding a new page, WebParts.aspx, to your project, and add a link to that page on your default.aspx page.
Creating Zones
A page that uses Web Parts is divided into zones: areas of the page that can contain content. It is typical (though certainly not required) to organize these zones using tables. To see a simple example of this at work, follow these steps:
1. Open the WebParts section of your Toolbox, and drag a WebPartManager onto your page, as shown in Figure 2.

Figure 2. Web Parts toolbox
The job of the WebPartManager is to track and coordinate all the Web Part controls on the page. It will not be visible when the page is running.
2. Add a new table, with two rows and three columns. Rearrange the columns so that they are not of even size.
3. Drag a WebPartZone into each of the six table cells. Each WebPartZone will have a default name (e.g., WebPartZone6) and a default heading. You can easily modify either or both of these properties in the properties window. Resize the WebPartZone controls as appropriate to the cell. Notice that within the zone is a shaded area with the text "Click here to add WebParts to the WebPartZone," as shown in Figure 3.

Figure 3. Creating zones
Adding Controls To Zones
4. Set the heading on the first Web Part control to News and drag a Label into the zone. The Label is wrapped in a Web Part control, and its title is set to Untitled, as shown in Figure 4.

Figure 4. Adding Controls
5. Switch to source view and change the title of the label to Today's News and the text to "<br/>Thousands die. Thousands more die at eleven.". Switch back to design view and drag a list box into WebPartZone3. Set the header text for the WebPartZone to "Sponsors." Click on the list box, and then on its smart tag to open the ListItemCollection Editor. Add a few items to the list box. Back in source view, set the Title property to "Our Sponsors" and run the application. You'll need to log in using one of the accounts you set up previously. Click on the link to the Web Parts page. You should see two Web Parts, complete with Minimize and Close commands, as shown in Figure 5.

Figure 5. The Web Parts live
Minimizing and Restoring
Click on the Minimize tag and the contents of the Web Part (e.g., the list box) collapses, and the link changes from "Minimize" to "Restore" as shown in Figure 6.

Figure 6. Closing and restoring
Exit the application. Start it again and sign back in, and navigate back to these pages. Aha! The minimized zone remains minimized. You'll find that the Web Parts are closely tied with personalization, and each individual's personalized Web Parts are persisted through the personalization database.
Allow Your Users to Move the Web Parts
Web Parts make it easy for you to allow the user to rearrange the page, placing, for example, News on the right and Sponsors on the left. To do so, drag a WebPartPageMenu control onto your page, as shown in Figure 7.

Figure 7. Adding a menu
Once this is in place, run the application. Log in and navigate to the Web Parts page. Click the drop-down list in the Web Parts Page Manager to set it to Design Page Layout and click the Change button. All of the WebPartZones become visible, and you can drag and drop WebParts from one zone to another by clicking on their titles and dragging them to different zones, as shown in Figure 8, in which you see the Today's News Web Part being dragged from Zone 1 (upper left) to Zone 4 (lower left).

Figure 8. Movable Web Parts
When you are done, return to the Web Parts Page Manager, set it to Browse This Page and click Change. The changes (moving from one zone to another) are reflected in the page. You may find it interesting to log into this page as another user (no changes reflected) and then again as the first user, to see that the changes are preserved from session to session. In a subsequent article, I'll look at how you can use Web Parts to create a portal application and how you can modify the Web Parts to create a customized look and feel.
|
Related Reading Programming .NET Windows Applications |
Jesse Liberty is a Microsoft Senior Program Manager where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight. Even before joining Microsoft, Jesse was well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight.
Read more Liberty on Whidbey columns.
Return to ONDotnet.com.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 21 of 21.
-
project
2008-03-20 23:27:16 lagudu [Reply | View]
how to create portal website in .net.
how to create webforms.
-
how to manually create webpartpagemenu
2007-06-14 04:37:02 Shivaji.N [Reply | View]
Hi,
how to create a webpartpagemenu manually using our own controls.
Post some sample code to create webpartpagemenu control.
-
regarding minimize controls in webparts
2007-06-14 04:15:48 Shivaji.N [Reply | View]
Hi,
I have included the titlebar in a label inside a webpartzone and im not able to view the controls.
How to view the controls minimize, close, restore at run time and how to invoke it.
Help me.
-
problem in login
2007-04-19 23:39:46 shhhh [Reply | View]
I have implemented web parts for an application in which i use login control,now i want to implement webparts in a live project in which we are already using our login form and our data base for login.how to implement it.
Give step by step help
thanks in advance
-
how to use other webpart contlos?
2007-03-09 23:46:48 zille [Reply | View]
hi....!
i have learnt alot 4m ur article...abt web controls...kindly provide an article in which u will guide us how to use other web part controls in the tool box in our real project applications...(e.g) catalog zone, page catalog part...and many more....
m waiting terribly 4ur next article...THX
-
how to use other webpart contlos?
2007-03-09 23:41:32 zille [Reply | View]
hi....!
i have learnt alot 4mmur article...abt web controls...kindly provide an article in which u will guide us how to other web p[art controls in the tool box in our real project application...e.g catalog zone, page catalog part...and many more....
m waiting terribly 4ur next article...THX
-
how to use other webpart contlos?
2007-03-09 23:40:46 zille [Reply | View]
hi....!
i have learnt alot 4mmur article...abt web controls...kindly provide an article in which u will guide us how to other web p[art controls in the tool box in our real project application...e.g catalog zone, page catalog part...and many more....
m waiting terribly 4ur next article...THX
-
web parts
2006-12-05 21:56:17 chetanmehta [Reply | View]
Hi
-I am using vs2005 but I am not able to find webpartpagemenu control in web parts in toolbox.
Can you help me out on that.. -
web parts
2006-12-06 05:10:34 Jesse Liberty [Reply | View]
This article is simply obsolete. The follow up article (http://www.ondotnet.com/pub/a/dotnet/2005/06/13/liberty.html) brings all this up to date.
You'll see that the missing control is not supplied by Microsoft but is easily created as a user control (I walk you through how to do it).
I apologize to you for the confusion.
-jesse
-
webpart control
2006-11-27 01:21:46 Prachi_Gurjar [Reply | View]
How to retain the webpart control, once closed by mistake? (at runtime).
I am not able to view the webpart once i closed it on runtime. -
webpart control
2007-04-09 06:40:42 shhhh [Reply | View]
hi write this code on ur webpart.aspx
<asp:CatalogZone ID="CatalogZone1" runat="server">
<ZoneTemplate>
<asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server">
<WebPartsTemplate>
<asp:button id=btnttms/>
</WebPartsTemplate>
</asp:DeclarativeCatalogPart>
<asp:PageCatalogPart ID="PageCatalogPart1" runat="server" />
</ZoneTemplate>
</asp:CatalogZone> -
webpart control
2006-11-27 05:30:56 Jesse Liberty [Reply | View]
To solve this, modify your Zone Template (where you currently have a DeclarativeCatalogPart, and add a PageCatalogPart (self-closing). This will modify your page, adding to the page catalog each web part that you close, allowing you to reopen it and assign it to any zone you choose, as shown here (http://www.jliberty.com/pages/WebPartsPageCatalogPart.htm)
Thanks!
-j
-j
-
Opening Editor, Catalogue zones on a new page
2006-10-18 20:45:24 Novich [Reply | View]
hi How to open a zone, eg: Editor Zone on a new page. At the moment I am opening it on the same page.
Regards,
Naveen...
-
webpart
2006-06-17 22:01:47 sawjansee [Reply | View]
i don know anythng abt webpart.. but i like the restoring and minimizing of web part zone.
i like to implement it in my project.
pls help me. for doing from the begining. -
webpart
2006-06-19 11:59:24 Jesse Liberty [Reply | View]
I'd be happy to help you but the article does try to take you from the beginning. Was there some part of this that you found confusing?
You may want to post a question on my support forum: http://forums.delphiforums.com/LibertyBooks
Thanks.
-
i can't change the "title" of the label
2006-04-27 17:10:55 mlcy [Reply | View]
i have followed the steps on this page, but when i tried to look for "title" property to change to "Today's News", i can't find it. i am using vs2005. where should i look? thanks. -
i can't change the "title" of the label
2007-03-15 00:07:47 onscreendotnet [Reply | View]
i can't explain you clearely so i ll send u wt i av done check it out.....in the label part....just type title="Today's news"
<ZoneTemplate>
<asp:Label ID="Label1" runat="server" Text="Australia beat scotland by 230 runs" title="Today's News"></asp:Label>
</ZoneTemplate>
-
Compilation Error
2006-03-27 20:58:51 asksam2002@msn.com [Reply | View]
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0234: The type or namespace name 'HttpProfile' does not exist in the namespace 'ASP' (are you missing an assembly reference?)
Source Error:
Line 33: {
Line 34:
Line 35: ASP.HttpProfile anonymousProfile = Profile.GetProfile(e.AnonymousId);
Line 36: if (anonymousProfile != null && anonymousProfile.ChosenBooks != null)
Line 37: {







http://www.ondotnet.com/pub/a/dotnet/2005/06/13/liberty.html
Thanks