Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows XP

Microsoft ASP.NET 4 : Developing a Web Part

- Windows 10 Product Activation Keys Free 2019 (All Versions)
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
7/14/2011 3:40:30 PM
Developing a Web Part is actually fairly straightforward and quite similar to developing a custom control . Instead of deriving a class from System.Web.UI.Controls.WebControl or System.Web.UI.Controls.CompositeControl, you derive a class from System.Web.UI.WebControls.WebParts.WebPart. From that point, you have the choice of either rendering HTML or composing a Web Part from other ASP.NET controls. The WebPart includes considerable functionality for integrating with the Web Part architecture.

For example, in the next example, the navigation URL and display name properties of the hyperlink Web Part are exposed as properties that the end user can modify through the PropertyGridEditorPart.

The following example illustrates how to create a hyperlink Web Part that you can add to the Links WebPartZone in the UseWebParts project. Although you could add a regular HyperLink control to the catalog, typical controls don't provide the same support for the user to modify the links. For example, when you edited the HyperLink controls in the previous example, all you could do was move them around in the Links Web Part. To provide your Web application users with additional properties they can configure, the links need to be represented as Web Parts in their own right.

Developing the HyperLinkWebPart

  1. Add a new project to the UseWebParts solution. Make it a class library and name the library WebPartLib. Visual Studio asks you to name the file, and the name you choose also becomes the name of the first class placed in the library. Name the file HyperLinkWebPart.cs. (Visual Studio will name the class HyperLinkWebPart.)

  2. Make a reference to the System.Web assembly in the new child project. Right-click the WebPartLib node in Solution Explorer and click the Add Reference option to add the System.Web assembly.

  3. Derive the new class from System.Web.UI.WebControls.WebParts.WebPart by adding it to the inheritance list, as shown here:

    using System;
    using System.Collections.Generic;

    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    namespace WebPartLib
    {

    public class HyperLinkWebPart : WebPart
    {

    }
    }

  4. Add two string member variables to the HyperLinkWebPart class—one to represent the display name of the Web Part and the other to represent the actual URL. Initialize them with reasonable values:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    namespace WebPartLib
    {

    public class HyperLinkWebPart :
    WebPart
    {

    string _strURL = "http://www.microsoft.com";
    string _strDisplayName = "This is a link";
    }
    }

  5. Add a field of type HyperLink to the class. The Web Part uses the existing functionality of the HyperLink control. Override CreateChildControls to create an instance of HyperLink and add it to the HyperLinkWebPart controls collection. Initialize the HyperLink.Text property to the member variable representing the display name. Initialize the HyperLink.NavigateUrl property to the member variable representing the URL:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    namespace WebPartLib
    {

    public class HyperLinkWebPart : WebPart
    {

    HyperLink _hyperLink;

    string _strURL = "http://www.microsoft.com";
    string _strDisplayName = "This is a link";
    protected override void CreateChildControls()
    {
    _hyperLink = new HyperLink();
    _hyperLink.NavigateUrl = this._strURL;

    _hyperLink.Text = this._strDisplayName;
    this.Controls.Add(_hyperLink);
    base.CreateChildControls();
    }
    }
    }


  6. Finally, expose the URL and the display name as properties so that the Web Parts architecture can understand and work with them. To allow the exposed properties to work with the Web Parts architecture through the PropertyGridEditorPart that you add later, be sure to adorn the properties with the attributes Personalizable, WebBrowsable, and WebDisplayName, as shown here:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    namespace WebPartLib
    {

    public class HyperLinkWebPart :
    System.Web.UI.WebControls.WebParts.WebPart
    {

    HyperLink _hyperLink;

    string _strURL = "http://www.microsoft.com";
    string _strDisplayName = "This is a link";
    [Personalizable(), WebBrowsable, WebDisplayName("Display Name")]
    public string DisplayName
    {
    get
    {
    return this._strDisplayName;
    }
    set
    {
    this._strDisplayName = value;
    if (_hyperLink != null)
    {
    _hyperLink.Text = this.DisplayName;
    }
    }
    }
    [Personalizable(), WebBrowsable, WebDisplayName("URL")]
    public string URL
    {
    get
    {
    return this._strURL;
    }


    set
    {
    this._strURL = value;
    if (_hyperLink != null)
    {
    _hyperLink.NavigateUrl = this.URL;
    }

    }
    }

    protected override void CreateChildControls()
    {
    _hyperLink = new HyperLink();
    _hyperLink.NavigateUrl = this._strURL;
    _hyperLink.Text = this._strDisplayName;
    this.Controls.Add(_hyperLink);
    base.CreateChildControls();
    }
    }
    }


  7. Compile the WebPartLib project. Note that this adds the new HyperLinkWebPart Web Part to the Toolbox. You need that in the next step.

  8. Now add the HyperLinkWebPart to the catalog. The Web Part should already be in the Toolbox.

  9. Put the CatalogZone into Edit Templates mode by clicking the small arrow in the Web Template. Then, drag the HyperLinkWebPart into the CatalogZone, just as you did earlier with the TextBox, as shown here:



  10. Add a title to the new catalog item. Switch to the source code window in Visual Studio. In the markup (in the Source view), add a title to the new control:

    <ZoneTemplate>
    <asp:DeclarativeCatalogPart
    ID="DeclarativeCatalogPart1" runat="server">
    <WebPartsTemplate>
    <cc1:HyperLinkWebPart
    Title="A HyperLink"
    ID="HyperLinkWebPart1"
    runat="server" />
    <asp:TextBox ID="TextBox1"
    Title="A TextBox"
    runat="server">
    </asp:TextBox>
    </WebPartsTemplate>
    </asp:DeclarativeCatalogPart>
    </ZoneTemplate>

    The HyperLinkWebPart should now appear in the catalog with a title, as shown here:



  11. Add a PropertyGridEditorPart to the EditorZone on the page. Just pick one out of the Toolbox and drop it onto the EditorZone, as shown in the following graphic:



  12. Surf to the Web site. Put the page in Catalog mode by selecting Catalog from the Switch Display Mode drop-down list.



  13. Select A Hyper Link in the Catalog Zone, and add it to the Links Web Part Zone.

  14. Put the Web Parts Page into Edit mode by selecting Edit from the Switch Display Mode drop-down list. Click the arrow in the upper-right corner of the newly added link.

  15. Select Edit to edit this link. You should see the Editor Zone appear, along with the new Property Grid showing text boxes for editing the DisplayName and URL. The default DisplayName and URL appear in the text boxes—just type in new values.



  16. Type in a new DisplayName and a new URL. (The example points to www.asp.net.) Select OK. The browser should now show the new properties for the HyperLinkWebPart.



    You should be able to surf to the site represented by the link.


Other -----------------
- Microsoft ASP.NET 4 : The Web Parts Architecture
- Microsoft ASP.NET 4 : Handlers and Session State & Generic Handlers (ASHX Files)
- Microsoft ASP.NET 4 : HTTP Handlers - Handlers and IHttpHandler
- Microsoft ASP.NET 4 : HTTP Handlers - The Built-in Handlers
- Microsoft ASP.NET 4 : ASP.NET Request Handlers
- Microsoft ASP.NET 4 : HttpModules (part 2) - Seeing Active Modules & Storing State in Modules
- Microsoft ASP.NET 4 : HttpModules (part 1) - Existing Modules & Implementing a Module
- Microsoft ASP.NET 4 : The HttpApplication Class and HTTP Modules - Overriding HttpApplication
- Microsoft ASP.NET 4 : Diagnostics and Debugging - Error Pages
- Microsoft ASP.NET 4 : Diagnostics and Debugging - Debugging with Visual Studio
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
Celebrity Style, Fashion Trends, Beauty and Makeup Tips.
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server