Fredrik Normén's Blog - NSQUARED²
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Microsoft Most Valuable Professional
     .Net Framework - ASP.Net - Architecture - Development
NOTE: This list of posts will only list the 15 latest posts, to see the rest, select from the Archives located in the menu to the left. The RSS will only list the 10 lastest posts.
How to create a WebPart in ASP.Net 2.0

Category:  ASP.Net 2.0

I walked through my history list of posts today, and I can’t find any post about how to create a WebPart, so I decided to write about that even if most of you probably already know how to create a WebPart.

 

Introduction

 

There are two types of WebParts, Custom WebParts and Generic WebParts. A custom WebPart is a custom control that inherits the System.Web.UI.WebControls.WebParts.WebPart class. A custom control that doesn’t inherit the WebPart class and will be added to a WebPartZone (WebPartZone is a zone on the page where WebParts can located. A WebPart can’t live outside a WebPartZone), the custom control will be wrapped to a Generic WebPart.

 

Create a GenericWebPart

 

To create a Generic WebPart programmatically, you have to use the WebPartManager’s (WebPartManager, is a control on the page, that is the “manager” over the WebParts. It must be located on the page where you want to use WebParts) CreateWebPart method:

 

GenericWebpart webPart = MyWebPartManager.CreateWebPart(LoadControl(“myUserControl.ascx”));

 

MyWebPartManager.AddWebPart(webPart, WebPartZone1, 0);

 

You can also declarative add a control into a WebPartZone:

 

<asp:WebPartZone ID="WebPartZone1" runat="server">

        <ZoneTemplate>

                <uc:MyUserControl ruant="server" .... />

        </ZoneTemplate>

</asp:WebPartZone>

 

The declarative added custom control will also be wrapped to a Generic WebParts. You can add the following attributes to your custom control to specify some WebPart specific options:

 

CatalogIconImageUrl

Description

Subtitle

Title

TitleIconImageUrl

TitleUrl

 

If the above attributes are added to the custom control, they will be added to the control’s Attributes collection and will be collected from the Generic WebPart’s properties with the same name:

 

<asp:WebPartZone ID="WebPartZone1" runat="server">

        <ZoneTemplate>

                <uc:MyUserControl ruant="server" Title="My Title" Description="Description" .... />

        </ZoneTemplate>

</asp:WebPartZone>

 

You can also implement the IWebPart interface, which also has the above properties. If the IWebPart interface is implemented, the Generic WebPart will get the value from the properties from the custom control instead of the control’s Attribute collection.

 

If you want to get the custom control wrapped to a Generic WebPart, you can use the GenericWebPart’s ChildControl method:

 

GenericWebPart myWebPart = MyWebPartManager1.WebParts[0] as GenericWebPart;

 

If (myWebPart != null)

     Control control = myWebPart.ChildControl;

 

 

Create a Custom WebPart

 

To create a Custom WebPart, you have to inherit the System.Web.UI.WebControls.WebParts.WebPart class:

 

using System.Web.UI.WebControls.WebParts;

 

public class MyWebPart : WebPart

{

}

 

A Custom WebPart is similar to a Custom Control, where you can either render the content of the control by over riding the Render method:

 

protected override void Render(HtmlTextWriter writer)

{

   writer.WriteBeginTag("h1");

   writer.Write("My WebPart");

   writer.WriteEndTag("h1");

}

 

or add controls, by overriding the CreateChildControl method:

 

protected override void CreateChildControls()

{

   this.Controls.Clear();

   this.Controls.Add(new LiteralControl(”<h1>My WebPart</h1>”));

}

 

The WebPart class has several members, for example Title, for setting or by getting the title of the WebPart, Width and Height to set the size of the WebPart etc. I will not write about those members in this post, you can find all of them in the documentation.

 

 

The WebPart’s Personalization feature

 

The WebPart framework can personalize the WebParts on a page. The WebPart’s Personalization feature is a provider based solution, so you can create your own personalization provider by inherit the PersonalizationProvider class. Every property added to your WebPart class marked with the PersonalizableAttribute will be automatically saved or collected by the WebPart’s Personalization feature. There are two personalization scopes that could be specified for the PersonalizableAttribute, shared and user scope. With share scope, you will share all the settings made for the WebParts on a page with everyone. With user scope, every user can has its own setting for the WebParts. The following code has two properties, one for user scope, and the other for shared scope:

 

[Personalizable(PersonalizationScope.User), WebBrowsable]

public string Message

{

   get { return this._privateMessage; }

   set { this._privateMessage = value; }

}

 

 

[Personalizable(PersonalizationScope.Shared), WebBrowsable]

public string PublicMessage

{

    get { return this._publicMessage; }

    set { this._publicMessage = value; }

}

 

The above example has a WebBrowsableAttribute. With the WebBrowsableAttribute, you can specify if the property should be displayed in a PropertyGridEditorPart. To change the settings for WebParts on a page, for example in which zone a WebParts should exist and the size of the WebPart etc, you use EditorParts. An EditorPart must exist inside of an EditorZone. The PropertyGridEditorPart will be used to display the properties you have added to your WebPart class and marked with the WebBrowsableAttribute. By default the PropertyGridEditorPart will display the properties with its own names. By changing the names to be displayed for your properties in the PropertyGridEditorPart, you can use the WebDescription and WebDisplayName attribute:

 

[Personalizable(PersonalizationScope.User),

WebDescription("Private message description"),

WebDisplayName("Private message"),

WebBrowsable]

public string Message

{

    get { return this._privateMessage; }

    set { this._privateMessage = value; }

}

 

 

[Personalizable(PersonalizationScope.Shared),

WebDescription("Public message description"),

WebDisplayName("Public message"),

WebBrowsable]

public string PublicMessage

{

    get { return this._publicMessage; }

    set { this._publicMessage = value; }

}

 

The WebDescriptionAtrtibute is used to describe the property, and the WebDisplayName is the name displayed in the PropertyGridEditorPart for the property. Now when you have the basic understanding of how to create a WebPart, let’s take a look at some code.

 

The following code is an example of a WebPart:

 

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

 

public class MyWebPart : WebPart

{

    private string _privateMessage = "Private message";

    private string _publicMessage = "Public message";

   

    public MyWebPart()

            {

    }

 

    protected override void CreateChildControls()

    {

        this.Controls.Clear();

 

        this.Controls.Add(new LiteralControl("Private Message: " + this._privateMessage + "<br>"));

        this.Controls.Add(new LiteralControl("Public Message: " + this._publicMessage + "<br>"));

    }

 

 

    [Personalizable(PersonalizationScope.User),

    WebDescription("Private message description"),

    WebDisplayName("Private message"),

    WebBrowsable]

    public string Message

    {

        get { return this._privateMessage; }

        set { this._privateMessage = value; }

    }

 

 

    [Personalizable(PersonalizationScope.Shared),

    WebDescription("Public message description"),

    WebDisplayName("Public message"),

    WebBrowsable]

    public string PublicMessage

    {

        get { return this._publicMessage; }

        set { this._publicMessage = value; }

    }

}

 

The following code will add a WebPart programmatically to a WebPartZone:

 

protected void Page_Load(object sender, EventArgs e)

{

    if (WebPartZone1.WebParts.Count == 0)

    {

        MyWebPart webPart = new MyWebPart();

        webPart.ID = "MyWebPart";

        webPart.Title = "MyWebPart";

 

        WebPartManager1.AddWebPart(webPart, WebPartZone1, 0);

    }

}

 

The code above will only add the WebPart if there is no WebParts added to the WebPartZone1 zone. When the personalization is enabled and a WebPart is added. The WebPart will automatically displayed next time the page is requested, you don’t need to add it again. By using the WebPartMananger’s AddWebPart method, you can add a WebPart to a WebPartZone. The following code is a ASP.Net page with a WebpartManager, WebPartZone, EditorPart with a PropertyGridEditorPart control and two button controls, one for make it possible to edit WebParts, and the other button will toggle the scope into Shared scope.

 

<%@ Page Language="C#" %>

 

<script runat="server">

   

    protected void Page_Load(object sender, EventArgs e)

    {

        if (WebPartZone1.WebParts.Count == 0)

        {

            MyWebPart webPart = new MyWebPart();

            webPart.ID = "MyWebPart";

            webPart.Title = "MyWebPart";

 

            WebPartManager1.AddWebPart(webPart, WebPartZone1, 0);

        }

    }

 

    protected void Button1_Click(object sender, EventArgs e)

    {

        WebPartManager1.DisplayMode = WebPartManager.EditDisplayMode;

    }

 

    protected void Button2_Click(object sender, EventArgs e)

    {

        WebPartManager1.Personalization.ToggleScope();

    }

   

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:WebPartManager ID="WebPartManager1" runat="server">

        </asp:WebPartManager>

        <asp:Button ID="Button1" runat="server" Text="Edit mode" OnClick="Button1_Click" />

        <asp:Button ID="Button2" runat="server" Text="Toggle to shared scope" OnClick="Button2_Click" /></div>

        <asp:WebPartZone ID="WebPartZone1" runat="server">

        </asp:WebPartZone>

        <asp:EditorZone ID="EditorZone1" runat="server">

        <ZoneTemplate>

            <asp:PropertyGridEditorPart ID="PropertyGridEditorPart1" runat="server" />

        </ZoneTemplate>

        </asp:EditorZone>

    </form>

</body>

</html>

 

To run this example, you have to add the MyWebPart class, into the Application_Code (before \Code) folder and create the Asp.Net page above. When you run the example, the MyWebPart will be added to the WebPartZone1. When you press the “Edit mode” button, you can make the PropertyGridEdtiorPart visible by selecting the Edit menu option for the WebPart you want to edit. When you are in edit mode, the PropertyGridEditorPart will only display the PublicMessage property, because the PrivateMessage Property is marked with the PersonalizationAttribute set to shared scope. To make the shared scope property available, you have to grant the users or roles that are allowed to toggle to the shared scope. You can do that by adding the following code to your web.config file:

 

<webParts>

   <personalization>

      <authorization>

         <allow verbs="enterSharedScope" users="*" />

      </authorization>

   </personalization>

</webParts>

 

The <allow> element will allow all users to enter shared scope. If you press the “Toggle to shared scope” button, you will know see both properties of MyWebPart. The value you enter for the PublicMessage property will be shared among all users that visit the page. The value you enter forthe PrivateMesssage in user scope, will only be displayed for the current logged in user (every user will have its own value if they change the value of the PublicMessage property).

 

Summary

 

In this post you have learned how to create a Generic Webpart and a Custom Webpart. You have also learned about some of the attributes that could be added to the WebPart, such as the PersonlizableAttribute to specify if the properties value should be handled by the Webpart Personalization feature etc.

 

Other post on my blog about the WebPart framework

 

The following posts on my blog could give you some more information about how to use the Webpart framework. Some of the posts can be out of date because they were written before Beta 1 of VS 2005. But you will still get some basic understanding out from the posts.

 

Import and Export WebParts

Show and Hide WebParts based on roles

GenericWebPart

How to build a EditorPart

CatalogPart example

WebPart that grabs content from an URL

Restore the personalization

 

Posted: Wednesday, November 03, 2004 - 18:59 GMT+1    Print     E-mail    Comments (20)
Feedback
re: How to create a WebPart in ASP.Net 2.0 - Chris

Fredrik

Using PortalManager.AddWebPart() within Page_Load does not allow the WebPart to be persisted into Personalization state. It simply disappears the next time the page is loaded.

Chris
Posted: Friday, November 05, 2004 - 17:26 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - Fredrik Normén

Chris:

Not if personalization is enable.
Posted: Friday, November 05, 2004 - 18:39 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - Chris

Hi Fredrik

I have personalization enabled (it is on by default?) and am using SQLPersonalizationProvider. My authentication mode is set to "Windows" and I know that the correct Profile information is being saved for different users.

Could you possibly send me some simple code that demonstrates the .AddWebPart() method during Page_Load() that persists state across restarts? I'd really appreciate it as this is a showstopper for my ASP.NET 2.0 development at the moment.

I just can't get it to work!

Thanks,
Chris
Posted: Monday, November 08, 2004 - 11:01 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - Chris

Fredrik

Actually, as a workaround, I'm adding the webpart via a linkbutton_click() event handler on the page. This works.

My custom WebPart derives from WebPart, and has a KeyedCollection<T> public property that I've marked as Personalizable/WebBrowsable. Again, this is persisted if modified within a click() event handler, but not if modified during Page_Load().

Any ideas?

Apologies if I'm missing something really obvious here, but like I said it's really important for me.

Thanks,
Chris
Posted: Monday, November 08, 2004 - 11:18 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - Chris

Doh.

I'm almost ashamed to admit my oversight.
I wasn't calling SetPersonalizationDirty() on the WebPart after changing personalizable properties.

Still doesn't explain the AddWebPart() issue though... ;o)

Chris
Posted: Monday, November 08, 2004 - 18:48 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - Fredrik Normén

Chris:

The code in the post will only add a WebPart for the first time the page is requested, the next time it will not. You can run the debugger and watch for your self. So try the code, if it will not work, please let me know. I have only tested the code in the latest CTP drop (October).
Posted: Monday, November 08, 2004 - 22:24 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - Roger

Can the WebDescription attribute work? in beta 1 ?
Posted: Tuesday, November 09, 2004 - 05:03 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - Fredrik Normén

Roger:

I'm not sure if it did, but it will work in the latest CTP drop.
Posted: Tuesday, November 09, 2004 - 06:17 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - Roger

thanks.I am looking for some way which can be used display friendly name in PropertyGridPart.
Posted: Wednesday, November 10, 2004 - 04:19 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - Fredrik Normén

Roger:

If you have beta 1 where WebDisplayName attribute is not added (if I remember correctly), you can’t display a friendly name for the properties in the PropertyGridEditor part. It will use the name of each property of your WebPart. You can create your own PropertyGridEditor part, but I think you should wait for Beta 2 or download the October CTP instead.
Posted: Wednesday, November 10, 2004 - 08:29 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - raj

hi
i am in begining stage
so i need ur help
Posted: Sunday, December 19, 2004 - 06:56 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - Prashant

Hi
I am Prashant. Currently working on ASP and VB6.0 and want to migrate to .Net, Please give me right direction. Which is my Start point,
Thank You
Prashant
Posted: Sunday, December 19, 2004 - 11:39 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - Craig

A picture is worth a thousand words I have no idea what you are talking about so as a new developer I must admit I am lost or should I cay "What are you talking about?"
Posted: Tuesday, December 21, 2004 - 17:37 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - John F. K.

I am a novice in this field
Posted: Friday, December 24, 2004 - 12:41 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - EdenMachine

This doesn't work if you are using a masterpage because you can't put the WebPartManager inside the aspx that's using a MasterPage. So you can't type "... MyWebPartManager.CreateWebPart ..." because "MyWebPartManager" doesn't exist in this context. Anyway around this?

Thanks,

Rich
Posted: Saturday, February 12, 2005 - 23:11 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - Fredrik Normén

EdenMachine:

You can get your WebPartManager by using the WebPartManager's GetCurrentWebPartManager method:

WebPartManager myManager = WebPartManager.GetCurrentWebPartManager(this);

myManager.CreateWebPart....
Posted: Sunday, February 13, 2005 - 15:52 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - EdenMachine

WOW!! Sweet - thank you!!!

Rich
Posted: Sunday, February 13, 2005 - 18:19 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - EdenMachine

GenericWebPart webPart = MyWebPartManager.CreateWebPart(LoadControl("myUserControl.ascx"));

This line causes this error in beta 2 just so you know.

The specified control of type 'ASP._myUserControl_ascx' does not have an ID.
Posted: Monday, March 21, 2005 - 16:52 GMT+1  
re: How to create a WebPart in ASP.Net 2.0 - EdenMachine

Oh, that's right - I think you have to do it this way so that it has an ID...

Control myControl = LoadControl("myUserControl.ascx");
myControl.ID = "mycontrol";

GenericWebPart webPart = myWebPartManager.CreateWebPart(myControl);
Posted: Tuesday, March 22, 2005 - 15:37 GMT+1  
Adding WebParts using WebPart Manager - Zainab

Hi,

I am creating dynamic page in ASP.NET which takes its data from the xml file. I want to add web parts to my page to have personalization

I tried to do like this but webParts are not displayed. I tried to add webParts using AddWebPart method of WebPartManager but wasn't successful.

Please guide me for the same.
Thank you.
Posted: Wednesday, January 14, 2009 - 07:17 GMT+1  

Add feedback
Subject:
Name:
URL:
Message:




Please enter the text from the image:   

 
   fredrik.nsquared2.com - 2007