WebPartZoneBase.OnCreateVerbs Method

Raises the CreateVerbs event.

Namespace:  System.Web.UI.WebControls.WebParts
Assembly:  System.Web (in System.Web.dll)

protected virtual void OnCreateVerbs(
	WebPartVerbsEventArgs e
)

Parameters

e
Type: System.Web.UI.WebControls.WebParts.WebPartVerbsEventArgs

An EventArgs that contains the event data.

You can override the OnCreateVerbs method in a derived WebPartZoneBase zone class to add additional WebPartVerb objects to the collection of standard verbs in your zone. You can do this by adding verbs to the collection in the Verbs property.

The following code example demonstrates a way to create a handler for the CreateVerbs event by overriding the OnCreateVerbs method to add a custom verb to a derived WebPartZoneBase zone.

The code example contains two source files. For the code example to run, you must compile this source code. You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. This code example uses dynamic compilation. For a walkthrough that demonstrates how to compile, see Walkthrough: Developing and Using a Custom Web Server Control.

The first part of the code example is the source code for a simple control derived from the WebPart class.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Security.Permissions;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Samples.AspNet.CS.Controls
{

	// This code snippet creates a simple Web Part control.
 	[AspNetHostingPermission(SecurityAction.Demand,
	  Level = AspNetHostingPermissionLevel.Minimal)]
	[AspNetHostingPermission(SecurityAction.InheritanceDemand,
	  Level = AspNetHostingPermissionLevel.Minimal)]
	public class SimpleControl : WebPart
	{

		private String _text = "Simple control text";

		public string Text
		{
			get
			{
				if (_text != null)
					return _text;
				else 
					return string.Empty;
			}
			set { _text = value; }
		}

		protected override void Render(System.Web.UI.HtmlTextWriter 
      writer)
		{
			writer.Write(this.Text);
		}
	}
}

The second part of the code example is the source code for a derived WebPartZoneBase zone that overrides the OnCreateVerbs method to add a custom verb to the zone. The code also creates a custom verb that appears in the verbs menu of WebPart controls contained in the zone, and the verb creates another copy of the current WebPart control.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Security.Permissions;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Samples.AspNet.CS.Controls
{
/* 
This code sample creates a Web Part zone and adds the 
"Copy Web Part" verb to any control in the zone.
*/
[AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
public class ZoneWithAddedVerb : WebPartZone
{

  protected override void OnCreateVerbs(WebPartVerbsEventArgs e)
  {
    List<WebPartVerb> newVerbs = new List<WebPartVerb>();
    newVerbs.Add(new CopyWebPartVerb(CopyWebPartToNewOne));
    e.Verbs = new WebPartVerbCollection(e.Verbs,newVerbs);
    base.OnCreateVerbs(e);
  }

  void CopyWebPartToNewOne(object sender, WebPartEventArgs e)
  {
    WebPartManager wpmgr = 
      WebPartManager.GetCurrentWebPartManager(Page);
    System.Web.UI.WebControls.WebParts.WebPart wp;
    Type tp = e.WebPart.GetType(); 
    wp = (System.Web.UI.WebControls.WebParts.WebPart)Activator.CreateInstance(tp);   
    wpmgr.AddWebPart(wp, e.WebPart.Zone, e.WebPart.ZoneIndex + 1);
  }
}
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  internal class CopyWebPartVerb : WebPartVerb
  {
    private const String _copyWebPartImageUrl = "~/CopyVerb.ico";

    internal CopyWebPartVerb(WebPartEventHandler serverClickHandler) :  
       base("MyVerb", serverClickHandler)
    { }
    public override string Text
    {
      get { return "Copy Web Part"; }
      set { ;}
    }
    public override string Description
    {
      get { return "This verb will copy this web part control " +
        "to a new one below"; }
      set { ; }
    }
    public override bool Enabled
    {
      get { return base.Enabled; }
      set { base.Enabled = value; }
    }

    public override string ImageUrl
    {
      get { return _copyWebPartImageUrl; }
      set { ; }
    }
  }
}

The third part of the code example is a Web page that hosts the derived zone and WebPart control. Near the top of the page is a Register directive to reference the derived zone component. If you load the page in a browser, the WebPart control appears in the zone. Click the verbs menu, then click the CopyWebPart verb, and it creates a copy of the control.

<%@ Page Language="C#" %>
<%@ Register TagPrefix="verbsample"  
    namespace="Samples.AspNet.CS.Controls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:WebPartManager ID="WebPartManager1" runat="server" />
    <verbsample:ZoneWithAddedVerb id="ZoneWithAddedVerb1" 
      HeaderText="Zone with Added Verb" runat="server">
        <ZoneTemplate>
           <verbsample:SimpleControl id="SimpleControl1" 
            title="Simple Control" runat="server" /> 
        </ZoneTemplate>
        </verbsample:ZoneWithAddedVerb>
     </form>
</body>
</html>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft