The following code example provides a complete code listing of the sample Web Parts page used to host the user control. It also provides code for the sample user control that is referenced in the page as a Web Parts control. Note that in order for the user control to be treated as a true Web Parts control capable of personalization, it must expose a public property using the [Personalizable] code attribute.
<!-- Web Parts page to host the user control -->
<%@ page language="VB" %>
<%@ register tagprefix="uc1" tagname="SearchUserControl"
src="searchusercontrol.ascx" %>
<html>
<head>
<title>Web Parts Demo Page</title>
</head>
<body>
<h1>Web Parts User Control Demonstration</h1>
<form runat="server" id="form1">
<asp:webpartmanager id="WebPartManager1" runat="server" />
<asp:webpartpagemenu
id="pagemenu1"
runat="server"
Mode="Menu"
MenuStyle-BorderWidth="1">
<browsemodeverb text="Browse" />
<designmodeverb text="Design" />
<editmodeverb text="Edit" />
</asp:webpartpagemenu>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td valign="top">
<asp:webpartzone id="SideBarZone" runat="server"
headertext="Sidebar Zone">
<zonetemplate>
<asp:label runat="server" id="linksPart" title="My Links">
<a href="www.asp.net">ASP.NET site</a>
<br />
<a href="www.gotdotnet.com">GotDotNet</a>
<br />
<a href="www.contoso.com">Contoso.com</a>
<br />
</asp:label>
</zonetemplate>
</asp:webpartzone>
</td>
<td valign="top">
<asp:webpartzone id="MainZone" runat="server"
headertext="Main Zone">
<zonetemplate>
<uc1:SearchUserControl id="searchPart" runat="server"
title="Search" />
</zonetemplate>
</asp:webpartzone>
</td>
<td valign="top">
<asp:editorzone id="EditorZone1" runat="server">
<zonetemplate>
<asp:appearanceeditorpart runat="server"
id="AppearanceEditorPart1" />
<asp:layouteditorpart runat="server"
id="LayoutEditorPart1" />
</zonetemplate>
</asp:editorzone>
</td>
</tr>
</table>
</form>
</body>
</html>
<!-- Web Parts user control -->
<%@ control language="VB" classname="SearchUserControl" %>
<script runat="server">
Private results As Integer
<Personalizable()> _
Property ResultsPerPage() As Integer
Get
Return results
End Get
Set(ByVal value As Integer)
results = value
End Set
End Property
</script>
<asp:textbox runat="server" id="inputBox"></asp:textbox>
<br />
<asp:button runat="server" id="searchButton" text="Search" />
<!-- Web Parts page to host the user control -->
<%@ page language="C#" %>
<%@ register tagprefix="uc1" tagname="SearchUserControl"
src="searchusercontrol.ascx" %>
<html>
<head>
<title>Web Parts Demo Page</title>
</head>
<body>
<h1>Web Parts User Control Demonstration</h1>
<form runat="server" id="form1">
<asp:webpartmanager id="WebPartManager1" runat="server" />
<asp:webpartpagemenu
id="pagemenu1"
runat="server"
Mode="Menu"
MenuStyle-BorderWidth="1">
<browsemodeverb text="Browse" />
<designmodeverb text="Design" />
<editmodeverb text="Edit" />
</asp:webpartpagemenu>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td valign="top">
<asp:webpartzone id="SideBarZone" runat="server"
headertext="Sidebar Zone">
<zonetemplate>
<asp:label runat="server" id="linksPart" title="My Links">
<a href="www.asp.net">ASP.NET site</a>
<br />
<a href="www.gotdotnet.com">GotDotNet</a>
<br />
<a href="www.contoso.com">Contoso.com</a>
<br />
</asp:label>
</zonetemplate>
</asp:webpartzone>
</td>
<td valign="top">
<asp:webpartzone id="MainZone" runat="server"
headertext="Main Zone">
<zonetemplate>
<uc1:SearchUserControl id="searchPart" runat="server"
title="Search" />
</zonetemplate>
</asp:webpartzone>
</td>
<td valign="top">
<asp:editorzone id="EditorZone1" runat="server">
<zonetemplate>
<asp:appearanceeditorpart runat="server"
id="AppearanceEditorPart1" />
<asp:layouteditorpart runat="server"
id="LayoutEditorPart1" />
</zonetemplate>
</asp:editorzone>
</td>
</tr>
</table>
</form>
</body>
</html>
<!-- Web Parts user control -->
<%@ control language="C#" classname="SearchUserControl" %>
<script runat="server">
private int results;
[Personalizable]
public int ResultsPerPage
{
get
{return results;}
set
{results = value;}
}
</script>
<asp:textbox runat="server" id="inputBox"></asp:textbox>
<br />
<asp:button runat="server" id="searchButton" text="Search" />
If you run the sample code and click the Display Mode page menu, you can alternate among the different personalization modes on the menu: Browse, Design, and Edit. In Design mode, you can arrange the layout of the page by clicking in the header of the two Web Parts controls and dragging them into other zones. In Edit mode, you can click the edit image in the header of either of the Web Parts controls, and then set various UI properties on that control.