Using
Web Parts
Web
Parts are components that user can show, hide, and move. One can add a catalog
from which user can add web part components.
Enable
user to arrange and edit Web Parts:
|
Mode
|
Description
|
|
BrowseDisplayMode
|
Default Mode.(Minimize, Restore, and
Close)
|
|
DesignDisplayMode
|
Minimize, Restore, Close, and Drag &
Drop
|
|
EditDisplayMode
|
Minimize, Restore, Close, Drag &
Drop, Title, Size, Direction, Window Appearance. You must AppearanceEditPart
and LayoutEditPart.
|
|
CatalogDisplayMode
|
Enables user to add web parts. Enabled
only when CatalogZone is added.
|
|
ConnectDisplayMode
|
Add a ConnectionZone that enables user to
dynamically link Web Parts.
|
WebPartManager1.DisplayMode = WebPartManager.ConnectDisplayMode;
Connecting
Web Parts:
Implement
the two classes that inherit from WebPart class. Then configure one as a
Provider and one as Consumer:
It’s
not necessary to inherit from WebPart, a consumer or provider can be simply a
user control or it can be a control.
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level =
AspNetHostingPermissionLevel.Minimal)]
public
interface IZipCode
{
string ZipCode { get; set;}
}
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level =
AspNetHostingPermissionLevel.Minimal)]
public
class ZipCodeWebPart : WebPart, IZipCode
// Make the implemented property
personalizable to save
// the Zip Code between browser sessions.
[Personalizable()]
public virtual string ZipCode
{
get { return zipCodeText; }
set { zipCodeText = value; }
}
// This is the callback method that returns
the provider.
[ConnectionProvider("Zip Code
Provider", "ZipCodeProvider")]
public IZipCode ProvideIZipCode()
{
return this;
}
}
[AspNetHostingPermission(SecurityAction.Demand,
Level =
AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal)]
public
class WeatherWebPart : WebPart
{
// This method is identified by the
ConnectionConsumer
// attribute, and is the mechanism for
connecting with
// the provider.
[ConnectionConsumer("Zip Code
Consumer", "ZipCodeConsumer")]
public void GetIZipCode(IZipCode Provider)
{
_provider = Provider;
}
Now
drag Both ZipCodeWebPart and WeatherWebPart on the Page.
Add
a static connection by updating the source code of WebPartManager1.
<asp:WebPartManager
ID="WebPartManager1" runat="server">
<StaticConnections>
<asp:WebPartConnection
ID="conn1"
ProviderID="ZipCodeWebPart1"
ProviderConnectionPointID="
ZipCodeProvider "
ConsumerID="WeatherWebPart1"
ConsumerConnectionPointID="
ZipCodeConsumer "
/>
</StaticConnections>
</asp:WebPartManager>
Creating
Dynamic Connection that user can break and create:
Drag
and drop the ZipCodeWebPart and WeatherWebPart on the page. Optionally
establish a static connection.
Drop
a ConnectionZone control to the
page. And set:
WebPartManager1.DisplayMode = WebPartManager. ConnectDisplayMode;
Now
user will be able to select the connect menu on either consumer web part or on
provider web part. The ConnectionZone editor will appear.
If
there is already a connection it will display it and will provider the option
to “Disconnect” that. Otherwise ConnectionZone will provide the option
to create the connection.
Personalizing
Web Parts:
Just
like Profile WebParts also automatically save their layout and other
information in Sql database so that when the user views the page next time
he/she will be presented with the web page with the same settings. It also
relies on session based on cookies or cookieless.
Enabling
Personalization for Custom Web Parts:
To
store your custom information in the Personalization database use [Personalizable()]
attribute.
[Personalizable()]
public virtual string ZipCode
{
get { return zipCodeText; }
set { zipCodeText = value; }
}
Enabling Shared Personalization: Allow a use to
modify the layout of the Page and that layout will be visible to all users. To
achieve it:
To disable Personalization for a web page simply set WepPartManage1.Personalization.Enabled = False or change the source: