
Referencing the User Control as a Web Parts Control
Now that you have created a user control with personalizable properties, you can create an ASP.NET Web page to host the user control as a Web Parts control. It is necessary to host the control as a Web Parts control in order for personalization features to work.
To reference the user control as a Web Parts control
In a text editor, create a new file and add the following page declaration to the beginning of the file:
<%@ page language="VB" %>
<%@ page language="C#" %>
Beneath the page declaration, add a declaration to reference the user control you created previously, as shown in the following example:
<%@ register tagprefix="uc1"
tagname="AccountUserControl"
src="AccountUserControlvb.ascx"%>
<%@ register tagprefix="uc1"
tagname="AccountUserControl"
src="AccountUserControlcs.ascx"%>
Beneath the control reference, add the following basic page structure to host the user control as a Web Parts control.
Note: |
|---|
For the user control to work as a Web Parts control, the page must contain an <asp:webpartmanager> element, and the user control must be contained within an <asp:webpartzone> element and a <zonetemplate> element in succession. |
<html>
<head runat="server">
<title>Personalizable User Control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:webpartmanager id="WebPartManager1" runat="server" />
<asp:webpartzone id="zone1" runat="server" headertext="Main">
<zonetemplate>
<uc1:AccountUserControl
runat="server"
id="accountwebpart"
title="Agent Information" />
</zonetemplate>
</asp:webpartzone>
</form>
</body>
</html>
Name the file Hostaccountcontrol.aspx and save it in the same directory as the user control.
You have now created a personalizable user control, and referenced it as a Web Parts control in a Web Forms page. The final step is to test your user control.
To test the personalizable user control
Load the Hostaccountcontrol.aspx page in a browser.
Enter values in the Name and Phone fields, and click the Save Form Values button.
Close the browser.
Load the page again in a browser.
The values you entered previously appear in the form. These are the values that were saved in the personalizable properties, and were restored from the database when you reloaded the page in the browser.
Enter new values in the form, but do not click the button to save them.
Close the browser.
Load the page again in a browser.
The original values you entered and saved in the personalized properties are the ones that reappear in the form, because you did not save the new values.