创建webpart 简单实例

通常有两种方法可以创建webpart,一种是在Web Part Zone采用用户控件,另一种通过 WebPart class基类创建。Web Part Framework 可以自动处理这两种方式。
下面是一个如何用这两种方法生成标准的Web Part。

Listing 1. HelloWorldPart.ascx

<%@ Control Language="VB" ClassName="HelloWorldPart" %>
            Hello World!

Listing 2. App_Code\HelloWorldPart.vb

Imports System
            Imports System.Web.UI
            Imports System.Web.UI.WebControls.WebParts
            Namespace myControls
            ''' <summary>
            ''' True Web Part
            ''' </summary>
            Public Class HelloWorldPart
            Inherits WebPart
            Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
            writer.Write("Hello World!")
            End Sub
            End Class
            End Namespace

注意:要创建App_code子目录,以便.net自动编译。

 

Listing 3. ShowHelloWorldPart.aspx

<%@ Page Language="VB" %>
            <%@ Register TagPrefix="custom" Namespace="myControls" %>
            <%@ Register TagPrefix="user" TagName="HelloWorldPart" Src="~/HelloWorldPart.ascx" %>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
            "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml" >
            <head id="Head1" runat="server">
            <style type="text/css">
            .column
            {
            float:left;
            width:45%;
            height:200px;
            margin-right:10px;
            border:solid 1px black;
            background-color: white;
            }
            html
            {
            background-color:#eeeeee;
            }
            </style>
            <title>Show Hello World Part</title>
            </head>
            <body>
            <form id="form1" runat="server">
            <asp:WebPartManager
            id="WebPartManager1"
            Runat="server" />
            <asp:WebPartZone
            id="WebPartZone1"
            CssClass="column"
            Runat="server">
            <ZoneTemplate>
            <user:HelloWorldPart
            id="HelloWorldPart1"
            Runat="server" />
            </ZoneTemplate>
            </asp:WebPartZone>
            <asp:WebPartZone
            id="WebPartZone2"
            CssClass="column"
            Runat="server">
            <ZoneTemplate>
            <custom:HelloWorldPart
            id="HelloWorldPart2"
            Runat="server" />
            </ZoneTemplate>
            </asp:WebPartZone>
            </form>
            </body>
            </html>

 

注意:要想看见Web Part Zones的菜单选项,请先用asp.net 2.0 的login注册。

标准的 Web Part 属性包括:

  • AuthorizationFilter  :让Web Part Manager 检查使用Web Parts的权限。

  • CatalogIconImageUrl :显示在Catalog Part的图标

  • Description

  • ExportMode :在Web Part 下载 XML文件 。分成 All, None, and NonSensitiveData三种方式。

  • Height

  • Subtitle :在标题后面再附加子标题

  • Title

  • TitleIconImageUrl

  • TitleUrl

  • Width

如:

<user:HelloWorldPart
id="HelloWorldPart1"
Title="Hello World"
Description="Displays Hello World"
Runat="server" />
 下面我们用这两种方式分别演示如何确定这些属性。
Listing 5. RandomQuotePart.ascx

<%@ Control Language="VB" ClassName="RandomQuotePart" %>
            <%@ Implements Interface="System.Web.UI.WebControls.WebParts.IWebPart" %>
            <%@ Import Namespace="System.Collections.Generic" %>
            <script runat="server">
            Private _title As String = "Random Quote"
            Private _titleUrl As String = "~/Help.aspx"
            Private _description As String = "Displays a random quote"
            Private _subTitle As String = "User Control Version"
            Private _catalogIconImageUrl As String = "~/Images/BigRandomQuote.gif"
            Private _titleIconImageUrl As String = "~/Images/SmallRandomQuote.gif"
            Public Property Title() As String Implements IWebPart.Title
            Get
            Return _title
            End Get
            Set(ByVal Value As String)
            _title = Value
            End Set
            End Property
            Public Property TitleUrl() As String Implements IWebPart.TitleUrl
            Get
            Return _titleUrl
            End Get
            Set(ByVal Value As String)
            _titleUrl = Value
            End Set
            End Property
            Public Property Description() As String Implements IWebPart.Description
            Get
            Return _description
            End Get
            Set(ByVal Value As String)
            _description = Value
            End Set
            End Property
            Public ReadOnly Property Subtitle() As String Implements IWebPart.Subtitle
            Get
            Return _subTitle
            End Get
            End Property
            Public Property CatalogIconImageUrl() As String Implements IWebPart.CatalogIconImageUrl
            Get
            Return _catalogIconImageUrl
            End Get
            Set(ByVal Value As String)
            _catalogIconImageUrl = Value
            End Set
            End Property
            Public Property TitleIconImageUrl() As String Implements IWebPart.TitleIconImageUrl
            Get
            Return _titleIconImageUrl
            End Get
            Set(ByVal Value As String)
            _titleIconImageUrl = Value
            End Set
            End Property
            Private Sub Page_PreRender()
            Dim quotes As New List(Of String)
            quotes.Add("All paid jobs absorb and degrade the mind -- Aristotle")
            quotes.Add("No evil can happen to a good man, either in life or after death -- Plato")
            quotes.Add("The only good is knowledge and the only evil is ignorance -- Plato")
            Dim rnd As New Random()
            lblQuote.Text = quotes(rnd.Next(quotes.Count))
            End Sub
            </script>
            <asp:Label
            id="lblQuote"
            runat="server" />

 

注意: <%@ Implements %>为User control i提供了 IWebPart 接口. 提供了标准属性的Default values 。

 

由于Web Part control 来自Web Part基类,必然包含IWebPart接口的所有属性。我们只需覆盖。

Listing 6. App_Code\RandomQuotePart.vb

Imports System
            Imports System.Collections.Generic
            Imports System.Web.UI
            Imports System.Web.UI.WebControls.WebParts
            Namespace myControls
            ''' <summary>
            ''' Displays a random quotation
            ''' </summary>
            Public Class RandomQuotePart
            Inherits WebPart
            Private _title As String = "Random Quote"
            Private _titleUrl As String = "~/Help.aspx"
            Private _description As String = "Displays a random quote"
            Private _subTitle As String = "True Web Part Version"
            Private _catalogIconImageUrl As String = "~/Images/BigRandomQuote.gif"
            Private _titleIconImageUrl As String = "~/Images/SmallRandomQuote.gif"
            Public Overrides Property Title() As String
            Get
            Return _title
            End Get
            Set(ByVal Value As String)
            _title = value
            End Set
            End Property
            Public Overrides Property TitleUrl() As String
            Get
            Return _titleUrl
            End Get
            Set(ByVal Value As String)
            _titleUrl = value
            End Set
            End Property
            Public Overrides Property Description() As String
            Get
            Return _description
            End Get
            Set(ByVal Value As String)
            _description = value
            End Set
            End Property
            Public Overrides ReadOnly Property Subtitle() As String
            Get
            Return _subTitle
            End Get
            End Property
            Public Overrides Property CatalogIconImageUrl() As String
            Get
            Return _catalogIconImageUrl
            End Get
            Set(ByVal Value As String)
            _catalogIconImageUrl = value
            End Set
            End Property
            Public Overrides Property TitleIconImageUrl() As String
            Get
            Return _titleIconImageUrl
            End Get
            Set(ByVal Value As String)
            _titleIconImageUrl = value
            End Set
            End Property
            Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
            Dim quotes As New List(Of String)()
            quotes.Add("All paid jobs absorb and degrade the mind -- Aristotle")
            quotes.Add("No evil can happen to a good man, either in life or after death --
             Plato")
            quotes.Add("The only good is knowledge and the only evil is ignorance -- Plato")
            Dim rnd As New Random()
            writer.Write(quotes(rnd.Next(quotes.Count)))
            End Sub
            End Class
            End Namespace

posted @ 2008-03-31 09:16  马建康  阅读(424)  评论(0)    收藏  举报