注意:此类在 .NET Framework 2.0 版中是新增的。
用作自定义 ASP.NET Web 部件控件的基类,为
Part 基类功能添加了一些附加用户界面 (UI) 属性、创建连接的能力和个性化行为。
命名空间:System.Web.UI.WebControls.WebParts
程序集:System.Web(在 system.web.dll 中)

语法
Public MustInherit Class WebPart
Inherits Part
Implements IWebPart, IWebActionable, IWebEditable
public abstract class WebPart : Part, IWebPart, IWebActionable, IWebEditable
public ref class WebPart abstract : public Part, IWebPart, IWebActionable, IWebEditable
public abstract class WebPart extends Part implements IWebPart, IWebActionable,
IWebEditable
public abstract class WebPart extends Part implements IWebPart, IWebActionable,
IWebEditable

备注

示例
下面的代码示例演示如何创建 WebPart 控件并在网页中引用该控件。
示例的第一部分包含名为 TextDisplayWebPart 的自定义 WebPart 控件的代码。此控件演示如何创建简单的自定义 WebPart 控件,使您能够使用 Web 部件控件集的功能。必须编译此源代码,代码示例才能运行。可以显式编译源代码,并将结果程序集放在网站的 Bin 文件夹或全局程序集缓存中。另外,也可将源代码放入站点的 App_Code 文件夹中,源代码将在运行时在此文件夹中进行动态编译。此代码示例假定您将源代码编译为一个程序集,将它放置在您的 Web 应用程序的 Bin 子文件夹中,然后使用网页中的 Register 指令引用该程序集。有关演示这两种编译方法的演练,请参见演练:开发和使用自定义服务器控件。
Imports System
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, _
Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class TextDisplayWebPart
Inherits WebPart
Private _contentText As String = Nothing
Private input As TextBox
Private DisplayContent As Label
Public Sub New()
Me.AllowClose = False
End Sub
<Personalizable(), WebBrowsable()> _
Public Property ContentText() As String
Get
Return _contentText
End Get
Set
_contentText = value
End Set
End Property
Protected Overrides Sub CreateChildControls()
Controls.Clear()
DisplayContent = New Label()
DisplayContent.Text = Me.ContentText
DisplayContent.BackColor = _
System.Drawing.Color.LightBlue
Me.Controls.Add(DisplayContent)
input = New TextBox()
Me.Controls.Add(input)
Dim update As New Button()
update.Text = "Set Label Content"
AddHandler update.Click, AddressOf Me.submit_Click
Me.Controls.Add(update)
ChildControlsCreated = True
End Sub
Private Sub submit_Click(ByVal sender As Object, _
ByVal e As EventArgs)
' Update the label string.
If input.Text <> String.Empty Then
_contentText = input.Text & "<br />"
input.Text = String.Empty
DisplayContent.Text = Me.ContentText
End If
End Sub
End Class
End Namespace
using System;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace Samples.AspNet.CS.Controls
{
[AspNetHostingPermission(SecurityAction.Demand,
Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level=AspNetHostingPermissionLevel.Minimal)]
public class TextDisplayWebPart : WebPart
{
private String _contentText = null;
TextBox input;
Label DisplayContent;
public TextDisplayWebPart()
{
this.AllowClose = false;
}
[Personalizable(), WebBrowsable]
public String ContentText
{
get { return _contentText; }
set { _contentText = value; }
}
protected override void CreateChildControls()
{
Controls.Clear();
DisplayContent = new Label();
DisplayContent.BackColor =
System.Drawing.Color.LightBlue;
DisplayContent.Text = this.ContentText;
this.Controls.Add(DisplayContent);
input = new TextBox();
this.Controls.Add(input);
Button update = new Button();
update.Text = "Set Label Content";
update.Click += new EventHandler(this.submit_Click);
this.Controls.Add(update);
ChildControlsCreated = true;
}
private void submit_Click(object sender, EventArgs e)
{
// Update the label string.
if (input.Text != String.Empty)
{
_contentText = input.Text + @"<br />";
input.Text = String.Empty;
DisplayContent.Text = this.ContentText;
}
}
}
}
package Samples.AspNet.JSL.Controls;
import System.*;
import System.Security.Permissions.*;
import System.Web.*;
import System.Web.UI.WebControls.*;
import System.Web.UI.WebControls.WebParts.*;
/** @attribute AspNetHostingPermission(SecurityAction.Demand, Level =
AspNetHostingPermissionLevel.Minimal)
*/
/** @attribute AspNetHostingPermission(SecurityAction.InheritanceDemand, Level =
AspNetHostingPermissionLevel.Minimal)
*/
public class TextDisplayWebPart extends WebPart
{
private String _contentText = null;
private TextBox input;
private Label displayContent;
public TextDisplayWebPart()
{
this.set_AllowClose(false);
} //TextDisplayWebPart
/** @attribute Personalizable()
@attribute WebBrowsable()
*/
/** @property
*/
public String get_ContentText()
{
return _contentText;
} //get_ContentText
/** @property
*/
public void set_ContentText(String value)
{
_contentText = value;
} //set_ContentText
protected void CreateChildControls()
{
get_Controls().Clear();
displayContent = new Label();
displayContent.set_BackColor(System.Drawing.Color.get_LightBlue());
displayContent.set_Text(this.get_ContentText());
this.get_Controls().Add(displayContent);
input = new TextBox();
this.get_Controls().Add(input);
Button update = new Button();
update.set_Text("Set Label Content");
update.add_Click(new EventHandler(this.Submit_Click));
this.get_Controls().Add(update);
set_ChildControlsCreated(true);
} //CreateChildControls
private void Submit_Click(Object sender, EventArgs e)
{
// Update the label string.
if (!(input.get_Text().Equals(""))) {
_contentText = input.get_Text() + "<br />";
input.set_Text("");
displayContent.set_Text(this.get_ContentText());
}
} //Submit_Click
} //TextDisplayWebPart
示例的第二部分演示如何在 ASP.NET 网页中引用 TextDisplayWebPart 控件。注意,许多不同的 WebPart 属性都可以通过声明方式分配给自定义控件。
<%@ page language="VB" %>
<%@ register tagprefix="aspSample"
Namespace="Samples.AspNet.VB.Controls"
Assembly="TextDisplayWebPartVB"%>
<html>
<head id="Head1" runat="server">
</head>
<body>
<form id="Form1" runat="server">
<asp:webpartmanager id="WebPartManager1" runat="server" />
<asp:webpartzone
id="WebPartZone1"
runat="server"
title="Zone 1"
PartChromeType="TitleAndBorder">
<parttitlestyle font-bold="true" ForeColor="#3300cc" />
<partstyle
borderwidth="1px"
borderstyle="Solid"
bordercolor="#81AAF2" />
<zonetemplate>
<aspSample:TextDisplayWebPart
runat="server"
id="textwebpart"
title = "Text Content WebPart" />
</zonetemplate>
</asp:webpartzone>
</form>
</body>
</html>
<%@ page language="C#" %>
<%@ register tagprefix="aspSample"
Namespace="Samples.AspNet.CS.Controls"
Assembly="TextDisplayWebPartCS"%>
<html>
<head id="Head1" runat="server">
</head>
<body>
<form id="Form1" runat="server">
<asp:webpartmanager id="WebPartManager1" runat="server" />
<asp:webpartzone
id="WebPartZone1"
runat="server"
title="Zone 1"
PartChromeType="TitleAndBorder">
<parttitlestyle font-bold="true" ForeColor="#3300cc" />
<partstyle
borderwidth="1px"
borderstyle="Solid"
bordercolor="#81AAF2" />
<zonetemplate>
<aspSample:TextDisplayWebPart
runat="server"
id="textwebpart"
title = "Text Content WebPart" />
</zonetemplate>
</asp:webpartzone>
</form>
</body>
</html>
<%@ page language="VJ#" %>
<%@ register tagprefix="aspSample"
Namespace="Samples.AspNet.JSL.Controls"
Assembly="TextDisplayWebPartJSL"%>
<html>
<head id="Head1" runat="server">
</head>
<body>
<form id="Form1" runat="server">
<asp:webpartmanager id="WebPartManager1" runat="server" />
<asp:webpartzone
id="WebPartZone1"
runat="server"
title="Zone 1"
PartChromeType="TitleAndBorder">
<parttitlestyle font-bold="true" ForeColor="#3300cc" />
<partstyle
borderwidth="1px"
borderstyle="Solid"
bordercolor="#81AAF2" />
<zonetemplate>
<aspSample:TextDisplayWebPart
runat="server"
id="textwebpart"
title = "Text Content WebPart" />
</zonetemplate>
</asp:webpartzone>
</form>
</body>
</html>

.NET Framework 安全性

继承层次结构

线程安全
此类型的任何公共静态(Visual Basic 中的
Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。

版本信息
.NET Framework
受以下版本支持:2.0

请参见