WebPartZoneBase.OnCreateVerbs(WebPartVerbsEventArgs) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
引发 CreateVerbs 事件。
protected:
virtual void OnCreateVerbs(System::Web::UI::WebControls::WebParts::WebPartVerbsEventArgs ^ e);
protected virtual void OnCreateVerbs (System.Web.UI.WebControls.WebParts.WebPartVerbsEventArgs e);
abstract member OnCreateVerbs : System.Web.UI.WebControls.WebParts.WebPartVerbsEventArgs -> unit
override this.OnCreateVerbs : System.Web.UI.WebControls.WebParts.WebPartVerbsEventArgs -> unit
Protected Overridable Sub OnCreateVerbs (e As WebPartVerbsEventArgs)
参数
包含事件数据的 EventArgs。
示例
下面的代码示例演示了一种通过重写 OnCreateVerbs 方法为 CreateVerbs 事件创建处理程序的方法,以便将自定义谓词添加到派WebPartZoneBase生区域。
该代码示例包含两个源文件。 若要运行代码示例,必须编译此源代码。 可以显式编译它,并将生成的程序集放入网站的 Bin 文件夹或全局程序集缓存中。 或者,可以将源代码放入站点的“App_Code”文件夹中,并在运行时动态编译源代码。 此代码示例使用动态编译。 有关演示如何编译的演练,请参阅 演练:开发和使用自定义 Web 服务器控件。
代码示例的第一部分是派生自 类的简单控件的 WebPart 源代码。
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);
}
}
}
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Collections.Generic
Imports System.Security.Permissions
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
' This code snippet creates a simple Web Part control.
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class SimpleControl
Inherits System.Web.UI.WebControls.WebParts.WebPart
Private _text As String = "Simple control text"
Public Property [Text]() As String
Get
If Not (_text Is Nothing) Then
Return _text
Else
Return String.Empty
End If
End Get
Set(ByVal value As String)
_text = value
End Set
End Property
Protected Overrides Sub Render(ByVal writer _
As System.Web.UI.HtmlTextWriter)
writer.Write(Me.Text)
End Sub
End Class
End Namespace
代码示例的第二部分是派生 WebPartZoneBase 区域的源代码,该代码将替代 OnCreateVerbs 方法以将自定义谓词添加到区域。 该代码还会创建一个自定义谓词,该谓词显示在区域中包含的控件的 WebPart 谓词菜单中,该谓词将创建当前 WebPart 控件的另一个副本。
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 { ; }
}
}
}
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Security.Permissions
Imports System.Collections.Generic
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
' This code sample creates a Web Part zone and adds the
' "Copy Web Part" verb to any control in the zone.
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class ZoneWithAddedVerb
Inherits WebPartZone
'public class ExtendedWebPartZoneBase
Protected Overrides Sub OnCreateVerbs(ByVal e _
As WebPartVerbsEventArgs)
Dim newVerbs As List(Of WebPartVerb) = _
New List(Of WebPartVerb)
newVerbs.Add(New CopyWebPartVerb(AddressOf CopyWebPartToNewOne))
e.Verbs = New WebPartVerbCollection(e.Verbs, newVerbs)
MyBase.OnCreateVerbs(e)
End Sub
Sub CopyWebPartToNewOne(ByVal sender As Object, _
ByVal e As WebPartEventArgs)
Dim wpmgr As WebPartManager = _
WebPartManager.GetCurrentWebPartManager(Page)
Dim wp As System.Web.UI.WebControls.WebParts.WebPart
Dim tp As Type = e.WebPart.GetType()
wp = CType(Activator.CreateInstance(tp), _
System.Web.UI.WebControls.WebParts.WebPart)
wpmgr.AddWebPart(wp, e.WebPart.Zone, e.WebPart.ZoneIndex + 1)
End Sub
End Class
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Friend Class CopyWebPartVerb
Inherits WebPartVerb
Private Const _copyWebPartImageUrl As String = "~/CopyVerb.ico"
Friend Sub New(ByVal serverClickHandler As WebPartEventHandler)
MyBase.New("MyVerb", serverClickHandler)
End Sub
Public Overrides Property [Text]() As String
Get
Return "Copy Web Part"
End Get
Set(ByVal value As String)
End Set
End Property
Public Overrides Property Description() As String
Get
Return "This verb will copy this web part control to a " _
& "new one below"
End Get
Set(ByVal value As String)
End Set
End Property
Public Overrides Property Enabled() As Boolean
Get
Return MyBase.Enabled
End Get
Set(ByVal value As Boolean)
MyBase.Enabled = value
End Set
End Property
Public Overrides Property ImageUrl() As String
Get
Return Me._copyWebPartImageUrl
End Get
Set(ByVal value As String)
End Set
End Property
End Class
End Namespace
代码示例的第三部分是承载派生区域和 WebPart 控件的网页。 页面顶部附近是引用 Register
派生区域组件的指令。 如果在浏览器中加载页面,则 WebPart 控件将显示在区域中。 单击谓词菜单,然后单击 CopyWebPart 谓词,然后创建控件的副本。
<%@ 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>
<%@ Page Language="VB" %>
<%@ Register TagPrefix="verbsample"
namespace="Samples.AspNet.VB.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>
注解
可以在派生WebPartZoneBase区域类中重写 OnCreateVerbs 方法,以将其他WebPartVerb对象添加到区域中的标准谓词集合。 可以通过向 属性中的 Verbs 集合添加谓词来执行此操作。