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...

Concept of App Domain in .Net

Creating Application Domains: Application domain is just like process, provides separate memory space, and isolates from other code. But it’s quite light weight. It also provides the following advantages: 1-       Reliability : If a domain crashes, it can be unloaded. Hence doesn’t affect the other assemblies. 2-       Efficiency : Loading all assemblies in one domain can be cumbersome and can make the process heavy but Appdomains are efficient in this manner. Important properties of AppDomain: ApplicationIdentity , ApplicationTrust , BaseDirectory , CurrentDomain , DomainManager , DomainDirectory , Evidence , FriendlyName , ID , RelativeSearchPath , SetupInformation , ShadowCopyFiles . Important methods of AppDomain: ApplyPolicy , CreateCOMInstanceFrom , CreateDomain , CreateInstance (Assembly). To create an AppDomain: AppDomain adomain = AppDomain.CreateDomain(“D”); To execute an assembly:...

ASP.NET Working With Data-Bound Web Server Controls

Suppose we have: List<Car> vCars = new List<Car>(); There are three types of databound controls: Simple databound controls(List, AdRotater), Composite data bound controls(GridView, DetailsView, FormView that inherit from CompositeDataBoundControl), and Hierarchal data bound controls (TreeView, Menu).   DataBoundControl has a DataBind method that can be used when data is ready. It calls DataBind for child controls as well. Page.DataBind() will call DataBind for all child controls. Using DataSource Objects:                                       BaseDataBound control exposes DataSource property that accepts objects that implement IEnumerable , IListSource , IDataSource , or IHierarchalDataSource . DataSourceID accepts ID of SqlDataSource . If both specified Data...