Skip to main content

ASP.NET Using Web Parts



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;

Containment Structure:





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:





Popular posts from this blog

Culture Information and Localization in .NET

Namespace: System.Globalization CultureInfo Class:                 It provides information like the Format of numbers and dates, Culture’s Calendar, Culture’s language and sublanguage (if applicable), Country and region of the culture. The Basic use of CultureInfo class is shown here: • How string Comparisons are performed • How Number Comparison & Formats are performed • Date Comparison and Formats. • How resources are retrieved and used. Cultures are grouped into three categories: Invariant Culture : It’s Culture Insensitive. It can be used to build some trial application. It can be also used to build an application with hard-coded expiry date that ignores cultures. But using it for every comparison will be incorrect and inappropriate. Neutral Culture : English(en), Frensh(fr), and Spanish(sp). A neutral culture is related to language but it’s not related to specific regi...

Using ADO.NET Transaction Object, Saving CLR Objects and SqlNotificationRequest

Serializable allows one transaction to complete before the other start. SqlConnectoin support named savpoint to roll back to; that’s an equaliant to save transaction command in MS SQL Server. Using TransactionScope Object : A transaction can’t span multiple connections.   Local and distributed transaction ares supported in 1.x you have to Enterpeise seveices library to regiser multiple connections and then call EnlistDistributedTransaction. Distributed Transaction Coordinator is required for Distributed transaction that’s available on windows 2000 & +. In 2.X and later use TransactionScope object. Serializable allows one transaction to complete before the other start. SqlConnectoin support named savpoint to roll back to; that’s an equaliant to save transaction command in MS SQL Server. Using TransactionScope Object : Dispoase of the transaction must be called to complete the transaction. Distributed Transaction : ...

Code Access Security in .Net

Namespace: System.Security What is Code Access Security? Code Access Security is a mechanism through which Developers and administrators can restrict code from accessing different resources , without caring about the users’ access level. You can also control resource that can’t be controlled through traditional RBS (Role Based Security), e.g. Web Requests and DNS requests etc. It can be only applied to Managed Applications. These restriction are applied not to the user instead to the Application, thus it does not require username or password. Evidence: It is the information that runtime gather about the assembly to determine which Code Groups the assembly belongs to. The following table shows the common types of evidence that a host can present to the runtime. Evidence Description Application directory The application's installation directory. Hash Cryptographic hash such as SHA1. Publisher Soft...