Introduction - ASP.NET AJAX and Web Parts
Ajax Web Parts Part 1 - Drag and Drop
Ajax Web Parts Part 2 - Web Part Connections
Ajax Web Parts Part 3 - Dynamic Web Parts
Ajax Web Parts Part 4 - Dynamic Web Part Connections
Download the source - Part 1
Download the source - Part 2
Download the source - Part 3
Download the source - Part 4
Modifying Lee’s code slightly, so that it derives from the Preview DLL’s WebPartManager, rather than the standard WebPartManager (to ensure cross browser drag and drop), gives the following:
namespace AjaxWebParts.Controls
{
public class DynamicWebPartManager
: Microsoft.Web.Preview.UI.Controls.WebParts.WebPartManager
{
public void AddDynamicWebPart(WebPart webPart,
Microsoft.Web.Preview.UI.Controls.WebParts.WebPartZone zone)
{
Internals.AddWebPart(webPart);
Internals.SetZoneID(webPart, zone.ID);
}
}
}
To use this new Web Part Manager we need to make the following changes:
In our DynamicWebParts class, make the following changes:
1. change the WebPartManager alias at the top of the line to become the following:
using WebPartManager = AjaxWebParts.Controls.DynamicWebPartManager;
2. change the addition of the dynamically created Web Part to the Web Part Zone to use the DynamicWebPartManger’s function. So the current
return itsManager.AddWebPart(genericWebPart, zone, 0);
is replaced by:
itsManager.AddDynamicWebPart(genericWebPart, zone);
return genericWebPart;
In the default.aspx page the following changes are required:
1. Add the following to the set of namespace registrations:
<%@ Register Namespace="AjaxWebParts.Controls" TagPrefix="awp" %>
2. Change the reference to the Web Part Manager from “cc1:WebPartManager” to “awp:DynamicWebPartManager”
After these changes have been made everything should now work as it should: the dynamically created Web Parts should now be able to be dragged between zones and multiple Web Parts should not appear when postbacks occur. All that is required now is to implement the connection between the dynamic Web Parts.
Now, when the Web Page is run, the dynamically generated Web Parts will be connected. Typing a value into the dynamic provider's text box and clicking the button will result in the value being displayed in the dynamically created consumer Web Part.
Now that we can dynamically create Web Parts, lets move on to create the rest of the page dynamically, starting with the Web Part Zones.
This is very straightforward – it’s simply a case of creating an instance of a WebPartZone and adding it to the page (note that the WebPartZone that gets created is from the Preview DLL, due to the alias that was declared).
The DynamicWebParts class member function therefore appears as follows:
Similarly, we can create the WebPartManager:
and the UpdatePanel:
Since all the page components can now be created dynamically, the static components in the page markup are no longer required. This page therefore simplifies down to:
Note that a PlaceHolder component has been added to give us somewhere to add our dynamic components.
In the Default.aspx.cs code behind file the Page_Init function now becomes:
And, since the page markup no longer defines a WebPartManager, this needs to obtained dynamically when needed. So the Page_load function now changes to become:
Click here to see this in action.
Putting this all together results in an ASP.NET AJAX page, featuring 2 Web Part Zones and 2 dragable, connected, Web Parts. Everything has now been created dynamically and is therefore just what’s required for populating tab pages.