
Creating a Simple Page with Web Parts
In this part of the walkthrough, you create a page that uses Web Parts controls to show static content. The first step in working with Web Parts is to create a page with two required elements. First, a Web parts page needs a WebPartManager control to coordinate all Web Parts controls. Second, a Web Parts page needs one or more zones, which are composite controls that contain WebPart or other server controls and occupy a specified region of a page.
Note |
|---|
| You do not need to do anything to enable Web Parts personalization; it is enabled by default for the Web Parts control set. When you first run a Web Parts page on a site, ASP.NET sets up a default personalization provider to store user personalization settings. For more information about personalization, see Web Parts Personalization Overview. |
To create a page for containing Web Parts controls
-
Close the default page and add a new page named WebPartsDemo.aspx.
-
Switch to Design view.
-
From the View menu, make sure that the Non-Visual Controls and Details options are selected so you can see layout tags and controls that do not have a UI.
-
Place the insertion point before the <div> tags on the design surface, and press ENTER to add a new line.
-
Position the insertion point before the new line character. From the Block Format list in the toolbar, select Heading 1 and in the heading, add the text Web Parts Demonstration Page.
-
From the WebParts tab of the Toolbox, drag a WebPartManager control onto the page, between the new line character and the opening <div> tag.
The WebPartManager control does not render any output, so it appears as a gray box on the designer surface.
-
Position the insertion point within the <div> tags.
-
In the Layout menu, click Insert Table, and create a new table that has one row and three columns. Click the Cell Properties button, select top from the Vertical align drop-down list, click OK, and click OK again to create the table.
-
Drag a WebPartZone control into the left table column. Right-click the WebPartZone control, choose Properties, and set the following properties:
ID: SidebarZone
HeaderText: Sidebar
-
Drag a second WebPartZone control into the middle table column and set the following properties:
ID: MainZone
HeaderText: Main
-
Save the file.
Your page now has two distinct zones that you can control separately. However, neither zone has any content, so creating content is the next step. For this walkthrough, you work with Web Parts controls that display only static content.
The layout of a Web Parts zone is specified by a <zonetemplate> element. Inside the zone template, you can add any ASP.NET control, whether it is a custom Web Parts control, a user control, or an existing server control. Notice that here you are using the Label control, and to that you are simply adding static text. When you place a regular server control in a WebPartZone zone, ASP.NET treats the control as a Web Parts control at run time, which enables Web Parts features on the control.
To create content for the main zone
-
In Design view, drag a Label control from the Standard tab of the Toolbox into the contents area of the zone whose ID property is set to MainZone.
-
Switch to Source view.
Notice that a <zonetemplate> element was added to wrap the Label control in the MainZone.
-
Add an attribute named title to the <asp:label> element, and set its value to Content. Remove the Text="Label" attribute from the <asp:label> element. Inside the <asp:label> element, add some text such as <h2>Welcome to my Home Page</h2>. Your code should look as follows:
<asp:webpartzone id="MainZone" runat="server" headertext="Main">
<zonetemplate>
<asp:label id="Label1" runat="server" title="Content">
<h2>Welcome to My Home Page</h2>
</asp:label>
</zonetemplate>
</asp:webpartzone> -
Save the file.
Next, create a user control that can also be added to the page as a Web Parts control.
To create a user control
-
Add a new Web user control to your site to serve as a search control named SearchUserControl.ascx, making sure that the Place source code in a separate file is cleared.
Note |
|---|
| The user control for this walkthrough does not implement actual search functionality; it is used only to demonstrate Web Parts features. |
-
Switch to Design view.
-
From the Standard tab of the Toolbox, drag a TextBox control onto the page.
-
Place the insertion point after the text box you just added, and press ENTER to add a new line.
-
Drag a Button control onto the page on the new line below the text box you just added.
-
Switch to Source view and then ensure that the source code for the user control looks like the following example:
<%@ control language="VB" classname="SearchUserControl" %>
<asp:textbox runat="server" id="TextBox1"></asp:textbox>
<br />
<asp:button runat="server" id="Button1" text="Search" />
<%@ control language="C#" classname="SearchUserControl" %>
<asp:textbox runat="server" id=" TextBox1"></asp:textbox>
<br />
<asp:button runat="server" id=" Button1" text="Search" />
-
Save and close the file.
Security Note |
|---|
| This control has a textbox that accepts user input, which is a potential security threat. User input in a Web page can potentially contain malicious client script. By default, ASP.NET Web pages validate user input to ensure that the input does not contain HTML elements or script. As long as this validation is enabled, you do not need to explicitly check for script or HTML elements in user input. For more information, see Script Exploits Overview. |
Now you can add Web Parts controls to the Sidebar zone. You are adding two controls to the Sidebar zone, one containing a list of links and another that is the user control you created earlier in the walkthrough. The links are added as a standard Label server control, similar to the way you created the static text for the Main zone. However, although the individual server controls contained in the user control could be contained directly in the zone (like the label control), in this case they are not. Instead, they are part of the user control you created in the previous procedure. This demonstrates a common way to package controls and extra functionality you want in a user control, and then reference that control in a zone as a Web Parts control.
At run time, the Web Parts control set wraps both controls with GenericWebPart controls. When a GenericWebPart control wraps a Web server control, the generic part control is the parent control, and you can access the server control through the parent control's ChildControl property. Using generic part controls enables standard Web server controls to have the same basic behavior and attributes as Web Parts controls that derive from the WebPart class.
To add Web Parts controls to the sidebar zone
-
Open the WebPartsDemo.aspx page.
-
Switch to Design view.
-
Drag the user control page you created, SearchUserControl.ascx, from Solution Explorer into the zone whose ID property is set to SidebarZone.
-
Save the WebPartsDemo.aspx page.
-
Switch to Source view.
-
Inside the <asp:webpartzone> element for the SidebarZone, add an <asp:label> element that contains links and in the user control tag, add a Title attribute with a value of Search, as shown in the following example:
<asp:WebPartZone id="SidebarZone" runat="server"
headertext="Sidebar">
<zonetemplate>
<asp:label runat="server" id="linksPart" title="My Links">
<a href="http://www.asp.net">ASP.NET site</a>
<br />
<a href="http://www.gotdotnet.com">GotDotNet</a>
<br />
<a href="http://www.contoso.com">Contoso.com</a>
<br />
</asp:label>
<uc1:SearchUserControl id="searchPart" runat="server"
title="Search" />
</zonetemplate>
</asp:WebPartZone> -
Save and close the file.
Now you can test your page.
To test the page
-
Load the page in a browser.
The page displays the two zones. The following screen shot shows the page.
Web Parts Demo page with two zones
In the title bar of each control is a downward arrow that provides access to a verbs menu of available actions you can perform on a control. Click the verbs menu for one of the controls, then click the Minimize verb and note that the control is minimized. From the verbs menu, click Restore, and the control returns to its normal size.