WebPart 类

提供用于创建Microsoft SharePoint Foundation Web 部件类的基类。

继承层次结构

System.Object
  System.Web.UI.Control
    System.Web.UI.WebControls.WebControl
      System.Web.UI.WebControls.Panel
        System.Web.UI.WebControls.WebParts.Part
          System.Web.UI.WebControls.WebParts.WebPart
            Microsoft.SharePoint.WebPartPages.WebPart
              

命名空间:  Microsoft.SharePoint.WebPartPages
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
<SupportsAttributeMarkupAttribute(False)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public MustInherit Class WebPart _
    Inherits WebPart _
    Implements INamingContainer, IAttributeAccessor, IConnectionData
用法
Dim instance As WebPart
[SupportsAttributeMarkupAttribute(false)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public abstract class WebPart : WebPart, 
    INamingContainer, IAttributeAccessor, IConnectionData

备注

SharePoint Foundation Web 部件基础结构是设计和构建相互重叠Microsoft ASP.NET Web 部件基础结构。Web 部件继承WebPartMicrosoft SharePoint Foundation (除了作为通用 Web 部件的用户控件) 中完全支持,并可以使用不仅ASP.NET应用程序中,还在SharePoint Foundation应用程序,无论SharePoint Foundation 。SharePoint FoundationWebPart类是专为 SharePoint 网站设计基础结构的一部分,可以仅在 SharePoint 网站中使用从此类继承的 Web 部件。

When creating new Web Parts, you have the option of creating Web Parts that inherit from System.Web.UI.WebControls.WebParts.WebPart (recommended) or Microsoft.SharePoint.WebPartPages.WebPart. The SharePoint FoundationWebPart class exists primarily for the purpose of backward compatibility and to provide a small set of features that are not available in the System.Web.UI.WebControls.WebParts.WebPart class.

以独占方式通过Microsoft.SharePoint.WebPartPages.WebPart提供的功能集是,如下所示:

  • 跨页连接

  • 区域之外的 Web 部件之间的连接

  • 客户端连接(Web 部件页服务组件)

  • 数据缓存基础结构,包括对数据库的缓存的能力

For more information, see Windows SharePoint Services and Web Parts.

示例

The following code example shows a simple Web Part that contains HtmlButton and HtmlInputText controls that allow the user to change the Web Part's Title property. For a step-by-step walkthrough of creating this Web Part, see Walkthrough: Creating a Basic SharePoint Web Part.

Imports System
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml.Serialization
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebPartPages
Imports Microsoft.SharePoint.Utilities
Imports System.Web.UI.HtmlControls

Namespace WebPartLibrary1
   ' This Web Part changes it's own title and implements a custom property.
   <XmlRoot([Namespace] := "WebPartLibrary1")>  _
   Public Class SimpleWebPart
      Inherits WebPart
      Private Const defaultText As String = "hello"
      Private [text] As String = defaultText

      ' Declare variables for HtmlControls user interface elements.
      Private _mybutton As HtmlButton
      Private _mytextbox As HtmlInputText      

      ' Event handler for _mybutton control that sets the
      ' Title property to the value in _mytextbox control.
      Public Sub _mybutton_click(sender As Object, e As EventArgs)
         Me.Title = _mytextbox.Value
         Try
            Me.SaveProperties = True
         Catch Else
            Caption = "Error... Could not save property."
         End Try
      End Sub      

      ' Override the ASP.NET Web.UI.Controls.CreateChildControls 
      ' method to create the objects for the Web Part's controls.      
      Protected Overrides Sub CreateChildControls()
         ' Create _mytextbox control.
         _mytextbox = New HtmlInputText()
         _mytextbox.Value = ""
         Controls.Add(_mytextbox)

         ' Create _mybutton control and wire its event handler.
         _mybutton = New HtmlButton()
         _mybutton.InnerText = "Set Web Part Title"
         AddHandler _mybutton.ServerClick, AddressOf _mybutton_click
         Controls.Add(_mybutton)
      End Sub      

      <Browsable(True), Category("Miscellaneous"), DefaultValue(defaultText), WebPartStorage(Storage.Personal), FriendlyName("Text"), Description("Text Property")>  _
      Public Property [Text]() As String
         Get
            Return [text]
         End Get

         Set
            [text] = value
         End Set
      End Property      

      Protected Overrides Sub RenderWebPart(output As HtmlTextWriter)
         RenderChildren(output)
         ' Securely write out HTML
         output.Write(("<BR>Text Property: " + SPEncode.HtmlEncode([Text])))
      End Sub
   End Class
End Namespace
//--------------------------------------------------------------------
// File: SimpleWebPart.cs
//
// Purpose: A sample Web Part that demonstrates how to create a basic
// Web Part.
//--------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.Web.UI.HtmlControls;

namespace WebPartLibrary1
{
    /// <summary>
    /// This Web Part changes its title and implements a custom property.
    /// </summary>
    [XmlRoot(Namespace="WebPartLibrary1")]    
    public class SimpleWebPart : WebPart
    {
        private const string defaultText = "hello";
        private string text=defaultText;

        // Declare variables for HtmlControls user interface elements.
        HtmlButton _mybutton;
        HtmlInputText _mytextbox;

        // Event handler for _mybutton control that sets the
        // Title property to the value in _mytextbox control.
        public void _mybutton_click (object sender, EventArgs e)
        {
            this.Title = _mytextbox.Value;
            try
            {
                this.SaveProperties=true;
            }
            catch
            {
                Caption = "Error... Could not save property.";
            }
        }

        // Override the ASP.NET Web.UI.Controls.CreateChildControls 
        // method to create the objects for the Web Part's controls.      
        protected override void CreateChildControls ()
        {         
            // Create _mytextbox control.
            _mytextbox = new HtmlInputText();
            _mytextbox.Value="";
            Controls.Add(_mytextbox);

            // Create _mybutton control and wire its event handler.
            _mybutton = new HtmlButton();
            _mybutton.InnerText = "Set Web Part Title";
            _mybutton.ServerClick += new EventHandler (_mybutton_click);
            Controls.Add (_mybutton);
        }

        [Browsable(true),Category("Miscellaneous"),
        DefaultValue(defaultText),
        WebPartStorage(Storage.Personal),
        FriendlyName("Text"),Description("Text Property")]
        public string Text
        {
            get
            {
                return text;
            }

            set
            {
                text = value;
            }
        }

        protected override void RenderWebPart(HtmlTextWriter output)
        {
            RenderChildren(output);
            // Securely write out HTML
            output.Write("<BR>Text Property: " + SPEncode.HtmlEncode(Text));    
        }
    }
}

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

WebPart 成员

Microsoft.SharePoint.WebPartPages 命名空间

继承层次结构

System.Object
  System.Web.UI.Control
    System.Web.UI.WebControls.WebControl
      System.Web.UI.WebControls.Panel
        System.Web.UI.WebControls.WebParts.Part
          System.Web.UI.WebControls.WebParts.WebPart
            Microsoft.SharePoint.WebPartPages.WebPart
              Microsoft.SharePoint.WebControls.ApplicationAssociationsViewWebPart
              Microsoft.SharePoint.WebControls.TopologyViewWebPart
              Microsoft.SharePoint.WebPartPages.AccessRequestsHideOldRequestsLink
              Microsoft.SharePoint.WebPartPages.AccessRequestsHideOldRequestsOnLoad
              Microsoft.SharePoint.WebPartPages.AggregationWebPart
              Microsoft.SharePoint.WebPartPages.BaseXsltDataWebPart
              Microsoft.SharePoint.WebPartPages.BlogAdminWebPart
              Microsoft.SharePoint.WebPartPages.BlogLinksWebPart
              Microsoft.SharePoint.WebPartPages.BlogMonthQuickLaunch
              Microsoft.SharePoint.WebPartPages.ChartViewWebPart
              Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
              Microsoft.SharePoint.WebPartPages.ImageWebPart
              Microsoft.SharePoint.WebPartPages.ListFormWebPart
              Microsoft.SharePoint.WebPartPages.ListViewWebPart
              Microsoft.SharePoint.WebPartPages.MembersWebPart
              Microsoft.SharePoint.WebPartPages.PageViewerWebPart
              Microsoft.SharePoint.WebPartPages.SimpleFormWebPart
              Microsoft.SharePoint.WebPartPages.SPTimelineWebPart
              Microsoft.SharePoint.WebPartPages.TilesViewWebPart
              Microsoft.SharePoint.WebPartPages.TitleBarWebPart
              Microsoft.SharePoint.WebPartPages.WikiContentWebpart
              Microsoft.SharePoint.WebPartPages.XmlWebPart