11.1. Using Server Controls and User Controls as Web Parts

Problem

You want to take advantage of web part functionality while leveraging standard ASP.NET server controls or perhaps your existing user controls.

Solution

Use the ASP.NET 2.0 membership and web part features by implementing the membership features described in Recipe 9.5 and modify web.config to configure the web part provider. In the pages that use web parts, add a WebPartManager control, add one or more WebPartZone controls, add a CatalogZone control, and add the desired server controls and user controls to the CatalogZone.

Add a <webParts> element to web.config as follows:

	<webParts>
		<personalization defaultProvider="AspNetSqlPersonalizationProvider">
			<providers>
				<remove name="AspNetSqlPersonalizationProvider"/>
				<add name="AspNetSqlPersonalizationProvider"
					type="System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider"
					connectionStringName="sqlConnectionString"
					applicationName="CH11ExamplesVB" />
			</providers>
			<authorization>
				<deny users="*" verbs="enterSharedScope" />
				<allow users="*" verbs="modifyState" />
			</authorization>
		</personalization>
	</webParts>

In the .aspx file for the pages that will use web parts:

  1. Add a WebPartManager control.

  2. Add one or more WebPartZone controls.

  3. Add a CatalogZone control along with PageCatalogPart and DeclarativeCatalogPart controls.

  4. Add the server controls and user controls you want to use as web parts to the DeclarativeCatalogPart control.

  5. Add a Customize button (or equivalent) to provide ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.