Here we look briefly at ASP.NET's Web Parts.
Well initially I started with a new Visual Studio 2010 Express Web Application project, which has Membership, Roles and Profiles enabled by default. But I was having some issues getting it to start from default settings. In the end I had to install the aspnetdb onto my local SQL Server Express database using the command line tool aspnet_regsql, rather than from the file system, and I also had to add a remove entry for 'LocalSQLServer' in my connectionStrings area in the web.config as I was getting a A network-related or instance-specific error. I encountered errors including 'schema compatible with schema version 1'. This particular issue was due to Personalization not been installed inside my aspnetdb, and was resolved with aspnet_regsql -A c. Also you must specify an application name in the providers such as the Membership Provider.
Well first of all we need a WebPartManager on our page to get WebParts working:
<asp:WebPartManager ID="WebPartManager1" runat="server">
Next we need to add some WebPartZones to the page. WebPartZones are like placeholders which act as a Web Part. We can put specific Web Part related controls into out WebPartZones, or we can even put standard ASP.NET controls into a WebPartZone. WebPartZones contain a ZoneTemplate which is where we declare the contents (or controls) for the WebPart, for instance we have a Literal control inside our WebPartZone here:
<asp:WebPartZone ID="WebPartZone1" HeaderText="Left Zone" runat="server" style="width: 300px; height: 300px; float: left;">
<asp:Literal ID="Literal_MyLiteral" runat="server" Text="Hello" />
If we take a look at our HTML which is produced from having just the above WebPartZone, we can see something like:
<table cellspacing="0" cellpadding="0" border="0" id="Table1" style="width: 300px; height: 300px; float: left;">
<td style="height:100%;"><table cellspacing="0" cellpadding="2" border="0" style="width:100%;height:100%;">
<td><table class="WebPartZone1_0" cellspacing="0" cellpadding="2" border="0" id="WebPart_gwpiuh" style="width:100%;">
<td><table cellspacing="0" cellpadding="0" border="0" style="width:100%;">
<td id="WebPartTitle_gwpiuh" style="width:100%;white-space:nowrap;"><span title="Untitled">Untitled</span> </td>
As we can see, alot of Tables used to structure the layout just for one WebPartZone which contained a Literal control with the text 'Hello'. I couldn't find a property such as .RenderOuterTable which can be found on other .NET 4.0 controls, but hopefully Microsoft would provide a layout which doesn't rely on tables in the future.
Web Parts plug into ASP.NET Personalization in that Personalization is used to store web parts information for each user.
The [Personalizable] attribute can be used to mark properties which have get and set accessors to be stored in the Personalization data store automatically.
Shared Personalization - Scope
It's possible to allow specific users to set web part information which is propagated to other users. Users or roles can be set in the web.config which set out who controls how the other user's see their web parts.
We need to put these settings into the webParts element in the web.config. After a little playing around I came out with the following config:
<allow verbs="modifyState" users="testtest"/>
<deny verbs="modifyState" users="*"/>
<allow verbs="enterSharedScope" users="testtest"/>
<allow verbs="enterSharedScope" users="testtest2"/>
<deny verbs="enterSharedScope" users="*"/>
This config says that a user called 'testtest' is the only user allowed to modify state and that users 'testtest' and 'testtest2' can enter shared scope but nobody else can. So user 'testtest' can make changes to the webparts, and user 'testtest2' will see those changes but not be able to edit them. So the webparts can be centrally controlled, something an admin person may like to do in an intranet scenario.
To get this config to work with the webparts, I had to try to make the WebPartManager switch to 'Shared' scope mode from 'User' scope mode. Depending on the user, this can throw an exception. So in the brief time I've been looking at this, I've just swallowed the exception for now, not ideal, there must be a better way to handle this, worth investigating if you have a little more time available.
protected void Page_Load(object sender, EventArgs e)
if (this.WebPartManager1.Personalization.Scope == PersonalizationScope.User)