1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Web Parts in a Web Application

Discussion in 'ASP.NET' started by MinalS, Jan 30, 2015.

  1. MinalS

    MinalS New Member

    Jul 8, 2014
    Likes Received:
    Trophy Points:
    The web parts helps user to create a personalized user interface in a consistent way. The information can be text, images, or any information. The web site becomes interactive and user friendly.

    The web parts help the developer to manage information in discrete units. The changes to the web parts like close move or rearrangement is possible. The preferences to the web parts can be saved by the user. The browser sessions are useful for it.

    Some of the advantages for the use of web parts are listed below:
    1. The web parts can be connected to each other. One of the connected web parts is known as provider and the consuming is called consumer. The appearance of the controls can be changed while displaying the data.
    2. User can add web parts to the web page and make changed to it. New web parts can be added to change the layout.
    3. The role based security can be assigned to the web parts. The web parts can be restricted access for certain users. It helps in creating a customized content through the security.
    4. The web part controls can be navigated to different pages or sites through the control settings.

    Web Parts Control Set

    ASP.NET provides user with the Web parts controls for implementing the web parts on the website. It contains a set of web parts that work together and helps user to create web parts pages. The web parts page is divided into zones that provide the design for adding the web parts.

    The Web Parts Control hierarchy is as shown below:


    There are many controls available for web parts in ASP.NET. Some of them are listed below:
    1. WebPartManager: It helps user to manage the web parts control on a web page. Every web parts page contains one WebPartManager control.
    2. CatalogZone: This control contains CatalogParts controls and is used to create a catalog web part. User can select a web part control to add a page.
    3. EditorZone: It has EditorPart controls that help user to edit and personalize the web part controls on the page.
    4. WebPartZone: It contains an overall layout for the web parts control containing main user interface of the page.
    5. ConnectionZone: It contains WebPartConnection controls and helps user to manage connections.
    6. CatalogPart: It contains the list of the web part controls that can be added on the web page.
    7. WebPartConnection: It provides a connection between two web parts controls on a page.
    8. EditorPart: It acts as a base class for the specified editor controls.

    Web Part Modes

    Web part pages con contains several display modes. Personalization is the advantage of using web parts. User can implement personalization allowing user to view the web page in different display modes.

    Through the display modes user can modify the web page. User can edit the web parts control, modify the layout of the web page, and add new web part controls through different display modes.

    User can add only one WebPartManager control on a web page. The implementation for the display modes is available on the manager control. There are five different display modes available as explained below.
    1. Browse: It is the default mode. It helps user to view the web parts on the web page. User can minimize or close the web part. User cannot restore the contents in browser mode.
    2. Edit: User can edit the web parts on the web page at runtime through the edit mode. The title, color or custom properties of the web parts can be set.
    3. Design: It helps user to rearrange the order of the web parts on the web page. The web parts can be dragged and dropped wherever needed.
    4. Catalog: This mode helps user to add a new web part on the web page. The order of the web parts can be changed. User can delete the web parts from the web page.
    5. Connect: It helps user to connect to two web parts controls on the web page.

    Creation of a web part control in ASP.NET

    For adding a web part on a web page user needs to add the WebPartManager control and one or more zone controls to it. The manager control is used to handle all the web parts controls on the web page. The WebPartZone controls acts as a container for the controls. The controls can be added on the web page.

    The steps for adding a web part control on a web page are as follows:
    1. In visual studio application, add the web form template.
    2. Navigate to the design view of the web page. Add the WebPartManager control from the WebParts section.
    3. In the source code window, add the following code.
      <asp:WebPartManager ID=”WebPartManager1” runat=”server”>
      <asp:DropDownList ID=”DropDownList1” runat=”server” AutoPostBack=”True” >
    4. Drag the EditorZone control from the WebParts section in the design view.
    5. Click on the smart tag button of the EditorZone1 control; select the AutoFormat from the start tag. The following dialog box appears.
    6. Select the Professional form and select the scheme section and click OK button.
    7. Drag and drop the WebPartZone control from the toolbox in the design view.
    8. Set the AutoFormat property for the control as Professional.
    9. Add the following code in the WebPartZone control.
      <asp:WebPartZone ID=”WebPartZone1” runat=”server”>
      	<asp:Label ID=”lbl1” runat=”server” Text=”Name”>
    10. Add another WebPartZone on the design view and add the following code.
      <asp:WebPartZone ID=”WebPartZone2” runat=”server”>
      		<asp:Label ID=”lbl2” runat=”server”>Links</asp:Label>
      		<a href=”http://www.google.com”>Google Site</a>
      		<a href=”http://www.gmail.com”>Gmail Site</a>
    11. The dropdownlist control generates a SelectedIndexChanged event handler. Add the following code to the event handler.
      using System;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      protected void DropDownList1_SelectedInexChanged ( object sender, EventArgs e )
      	WebPartManager wpm = WebPartManager.GetCurrentWebPartManager (Page);
      	string select =DropDownList1.SelectedValue;
      	WebPartDisplayMode mode = wpm.SupportedDisplayModes[select];
      	if ( mode!=null)
      		wpm.DisplayMode = mode;
    Web Parts Personalization

    In web applications, the user needs to modify, personalize the user interface. The personalization helps the properties or state of the web part controls to be saved in the storage.

    Features of ASP.NET Personalization

    1. The data stored in the personalization is long lasting as compared to the data stored in the view state.
    2. Personalization can be used within the WebPartZone. They access the functionality provided by the web parts.
    3. The user specific information is stored in a particular web page only.
    User can scope the use of personalization in a web page. The personalization changes can have two scopes as Shared or User. When the scope is shared, the changes applied to the page are for all the users. If the scope is User, the changes are applied to the specified user on the web page.

    The control visibility defines the availability of the control to the users. If the control is declared through the markup, it is a shared control. If the control is added through the application code, the visibility is checked by the current page scope.

    Components of Personalization

    1. WebPartManager: It is useful for managing all the web parts on a web page. The life cycle of data personalization is managed through it. The functionality, events occurring on the web page are managed though the WebPartmanager.

    The WebPartManager constructor is useful for initializing the new instance of the class.

    2. WebPartPersonalization: The logic required for the implementation of the personalization actions is defined. It is available in System.Web.UI.WebControls.WebParts namespace.
    The WebPartPersonalization constructor is used for initializing the new instance of the class.

    Some of the properties of the Personalization class are as mentioned below:
    1. CanEnterSharedScope: It returns a value indicating that the authorized user can enter the Shared scope.
    2. InitialScope: User can get or set the default personalization scope.
    3. ProviderName: It gets or sets the provider name for personalization
    4. Scope: It returns the current personalization scope for the WebPartManager control
    5. UserCapabilites: It gets or sets the user capabilities for the permitted user
    For enabling the personalization in a WebPartZone, the WebPartManager control is used for controlling the functionality.

    User must add the Personalizable attribute at the top of the property. The following code snippet demonstrates the functionality.
    public string DemoProp
    	Get{ ; }
    	Set{ ; }
    Web Parts Connections

    A web part connection is useful when user wants to connect sets of data from data sources. The web page contains several web parts. User needs to correlate the web parts with each other. The connection contains two controls as provider and consumer.

    The provider control establishes connections with several consumers at a same time. For creating a connection, the provider control defines the communication contract as an interface defining the data provided. The consumer control retrieves the data after obtaining connection.

    The connection point is defined by the web part for participating in a connection. The connection point contains the details such as control type, ID of the connection point object, data type control recognizes. There are two types of connection points known as consumer connection and provider connection points.

    The following steps are performed for creating connected web parts.
    1. Create the provider web part as a custom control. It is derived from the WebPart class. It defines a connection point as a method to which the ConnectionProvider attribute is applied.
    2. Create a consumer web part as a custom control. It is derived from the WebPart class. It defines a connection point as a method to which the ConnectionConsumer attribute is applied.
    3. The web parts are added to the Toolbox
    4. The web parts controls are added to the web parts page from the Toolbox. The ID and titles are assigned to the controls
    5. Navigate to the source view of the web parts page. Add the connection data to the WebPartManager control.
    6. The following code is added to the manager control.
      <asp:WebPartManager ID=”WebPartManager1” runat=”server”>
      	<asp:WebPartConnection ID=”con1” ProviderID=”Provider1”
      	ProviderConnectionPointID=”connect” ConsumerID=”Consumer1”
      	ConsumerConnectionPointID=”connect” />

    Web Parts Pages security

    The Web Parts controls are extended ASP.NET server controls; they are at a higher risk than the ASP.NET pages.

    The issues that occur due to the web parts in ASP.NET are as stated below:

    1. Importing Control Data

    The web parts contain the import feature where user can import an XML file containing the state and property data for the server control. The data can be shared and the complex controls can be configured very easily. The sensitive script code can be affected during importing.

    2. Personalization information

    Through personalization user can modify the layout, appearance, the control content on the web page. The data is stored in the database and is rendered as pages. The users can change the content of the site very easily. User must not add the personalization feature for all web pages. It must be used only for the required ones.

    3. Exporting the Control Data

    The export feature helps user to save the property and state data for a particular control in an XML file. The sensitive data can be exported through the description file.
    If users do not want to export the control data, the web server control to be exported is added with the Personalizable attribute.

    4. Minimal access to the Shared Personalization

    The shared scope helps user to access all the information that has been modified by administrator. As a result, all the changes are visible throughout the web page.
    To restrict the access, the selected user must be given access through the web site configuration file.

    5. Tested controls

    The controls that are added on the web parts application must be tested for its performance. If it has a poor performance it can result into several instances of the control.
    shabbir likes this.

Share This Page