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.
Add Verbs to the WebPart's menu.

Category:  ASP.Net 2.0

A WebPart has a menu with options like, minimizing or closing the WebPart. This menu items are called Verbs. The WebPart menu with verbs can be extended with your own verbs. If you create custom WebPart by inheriting the WebPart class, you can return your own verbs for your WebPart by overriding the Verbs property that returns a WebPartVerbCollection with your own WebPartVerb classes. The Verbs property is implemented from the IWebActionable interface. If you don’t want to create a custom WebPart by inherteting the WebParts class, you could instead implement the IWebActionable interface for your custom control to add additional verbs.

 

To create a verb, you need to use the WebPartVerb class. This class has three overloaded constructors:

 

WebPartVerb (String clientClickHandler)   

WebPartVerb (WebPartEventHandler serverClickHandler)   

WebPartVerb (WebPartEventHandler serverClickHandler, String clientClickHandler) 

 

clientClickHandler

 

The clientClickHanler is used to specify a client side handler for the WebPartVerb, that will be executed when the verb is selected from the WebPart menu.

 

serverClickHandler

 

The serverClickHanlder is used to specify a server side handler for the WebPartVerb.

 

The following code creates a WebPartVerb for maximizing the WebPart’s WebPart and its Zone, and use a server click handler:

 

WebPartVerb myVerb = new WebPartVerb(OnMaximize);

myVerb.Description = "Maximize the WebPart";

myVerb.Text = "Maximize";

 

The server click handler must have the same interface as the WebPartEventHandler delegate. The following code is the OnMazimize method that will be executed when the verb is clicked:

 

public void OnMaximize(object sender, WebPartEventArgs e)

{

      e.WebPart.Zone.Height = Unit.Percentage(100);

      e.WebPart.Zone.Width = Unit.Percentage(100);

 

      e.WebPart.Height = Unit.Percentage(100);

      e.WebPart.Width = Unit.Percentage(100);

 

      foreach (WebPart webPart in e.WebPart.Zone.WebParts)

      {

          if (!webPart.Equals(this))

              webPart.Hidden = true;

      }

}

 

The code above will get the WebPart that has the maximize verb and set the zone’s height and width to 100% where the WebParts belongs to, and will hide the rest of the WebParts in the Zone.

 

Note: If you change a WebPart or its Zone's state, and if the personalization feature is enabled for the WebPartManager, the state of the page will be saved, so next time a user visit the page, the WebPart will remain "maximized".

 

To add the WebPartVerb to the WebPart’s verbs menu, you override the Verbs property of the WebPart class, or as I wrote before, implement the IWebActionalbe interface. The Verbs property will return a read-only collection with WebPartVerbs. If you want to add WebPartVerbs to the collection, you need to pass an ICollection to the WebPartVerbCollection’s constructor. The following code overrides the Verbs property and add the maximize WebPartVerb from the code earlier to the WebPart’s verbs menu:

 

private WebPartVerbCollection _verbs;

   

public override WebPartVerbCollection Verbs

{

    get

    {

         if (this._verbs == null || this._verbs.Count < 1)

         {

             ArrayList verbs = new ArrayList();

 

             WebPartVerb myVerb = new WebPartVerb(OnMaximize);

             myVerb.Description = "Maximize the WebPart";

             myVerb.Text = "Maximize";

 

             verbs.Add(myVerb);

 

             this._verbs = new WebPartVerbCollection(verbs);

          }

 

        return this._verbs;

    }

}

 

When the WebPart is added to a Zone, the Verbs property will be called, and the verbs located in the WebPartVerbCollection will be added to the verbs menu of the WebPart.

 

The following code will use the clientClickHandler to execute a client-side script that in this case will display the id of the WebPart control that should be maximized:

 

WebPartVerb myVerb = new WebPartVerb("onMaximize('" + this.ClientID + "')");

myVerb.Description = "Maximize the WebPart";

myVerb.Text = "Maximize";

 

client-side script:

 

<script language=javascript>

 

    function onMaximize(object)

    {

        alert(object);

    }

 

</script>

 

The WebPartVerbs class has several properties, such as the ImageUrl to specify an icon for the menu item, the Enable property to enable or disable the verb or the Visible to hide the verb.

Posted: Tuesday, November 09, 2004 - 20:18 GMT+1    Print     E-mail    Comments (13)
   fredrik.nsquared2.com - 2007