介绍ASP.NET 2.0 Web Parts框架

Introducing the ASP.NET 2.0 Web Parts Framework

Stephen Walther

微软公司

2004-10
2005-03针对Beta 2的变化进行更新

适用于:

ASP.NET 2.0 Beta 2
ASP.NET框架
Web Parts

 

概要:Web Parts全套控件可以让你创作出可自定义的Web应用,比如:企业网门户应用。当你使用Web Parts建设一个网站,那么不论是站点管理员,还是个别用户都可以很轻易地自定义站点。Web Parts可以让站点变得更有亲和力。

源码下载:

目录

  • 介绍 
  • 建立Web Parts(正在翻译...)
  • 个性化Web Parts(正在翻译...)
  • 创建一个自定义Web Parts菜单(正在翻译...)
  • Web Parts间传递信息(正在翻译...)
  • 导入导出Web Part设置
  • 总结(正在翻译...)

介绍

ASP.NET 2.0中新的Web Parts框架给我们带来很多的好处,你可以建立可以被用户在运行时自定义的应用程序。用户可以通过拖拽的方式来重排页面各Web Parts元素;也可以通过一个或多个Web Parts分类控件来添加删除页面元素。

这篇文章向你介绍Web Part框架。本文不仅向你介绍Web Parts的基础内容,同时还讨论一些它的高级特性。例如:你可以从本文学习到怎样通过导入导出Web Parts而在应用程序间共享设置和怎样添加自定义菜单动词(verbs)到Web Parts;还可以学习到怎样通过连接一个页面中不同的Web Parts来实现它们之间的信息传递。

导入和导出web part设置

 

这是最后一部分了,我们将试着完成最后一个web part高级特性:怎么导入和导出web part设置。

在你想在不同web应用程序间,甚至是不同web站点间共享web part设置,那导入和导出web part设置将会是非常有用的。如果多于一个站点正使用着相当一致的web part(它不必是是完全一致的,但要有相同的属性),你就可以跨网站应用设置了。

修改后的FeaturedProdecutPart程序清单(Listing 11)被配置为允许用户导出web part设置。

Listing 11. FeaturedProductPart5.cs (C#)

using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
namespace myControls
{
    public class FeaturedProductPart5 : WebPart
    {
        const string connectionString =
        "Server=localhost;Trusted_Connection=True;Database=Northwind";
        const string selectString = "SELECT * FROM Products " +
        "JOIN Categories ON Products.CategoryID=Categories.CategoryID";
        private string _categoryName = "Beverages";
       
        public FeaturedProductPart5()
        {
            this.title = "Featured Product (Visual Studio 2005 Technical Articles)";
            this.ExportMode = WebPartExportMode.All;
        }
        [Personalizable, WebBrowsable]
        public string CategoryName
        {
            get { return _categoryName; }
            set { _categoryName = value; }
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            // Load Products into DataTable
            DataTable productTable = new DataTable();
            SqlDataAdapter dad = new SqlDataAdapter(selectString,
              connectionString);
            dad.Fill(productTable);
            // Filter the DataTable with a Category
            DataView productView = productTable.DefaultView;
            productView.RowFilter = "CategoryName='" +
              _categoryName + "'";
            if (productView.Count == 0)
                return;
            // Randomly select a row
            Random rnd = new Random();
            DataRowView row = productView[rnd.Next(productView.Count)];
            // Render the row
            writer.Write((string)row["ProductName"]);
            writer.Write(String.Format("- {0:c}", row["UnitPrice"]));
        }
    }
}

Listing 11. FeaturedProductPart5.vb (Visual Basic .NET)

Imports System.Collections
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI
Imports System.Web.UI.WebControls.WebParts
Namespace myControls
    Public Class FeaturedProductPart5
        Inherits WebPart
        Const connectionString As String = _
        "Server=localhost;Trusted_Connection=True;Database=Northwind"
        Const selectString As String = "SELECT * FROM Products " & _
        "JOIN Categories ON Products.CategoryID=Categories.CategoryID"
        Private _categoryName As String = "Beverages"
        Public Sub New()
            MyBase.Title = "Featured Product"
            MyBase.ExportMode = WebPartExportMode.All
        End Sub
        <Personalizable(), WebBrowsable()> _
        Public Property CategoryName() As String
            Get
                Return _categoryName
            End Get
            Set(ByVal value As String)
                _categoryName = value
            End Set
        End Property
        Protected Overrides Sub RenderContents(ByVal writer As _
          HtmlTextWriter)
            ' Load Products into DataTable
            Dim productTable As New DataTable()
            Dim dad As New SqlDataAdapter(selectString, _
              connectionString)
            dad.Fill(productTable)
            ' Filter the DataTable with a Category
            Dim productView As DataView = productTable.DefaultView
            productView.RowFilter = "CategoryName='" &
              _categoryName & "'"
            If productView.Count = 0 Then
                Return
            End If
            ' Randomly select a row
            Dim rnd As New Random()
            Dim row As DataRowView = _
              productView(rnd.Next(productView.Count))
            ' Render the row
            writer.Write(row("ProductName").ToString())
            writer.Write(String.Format("- {0:c}", row("UnitPrice")))
        End Sub
    End Class
End Namespace

看一下Listing 11里构造器(constructor)部分,可以知道构造器将ExportMode设置为WebPartExportMode.All,也就是将这个属性设置为就可以导出,那么web part就可以导出了。

这里还有另外一步要做,就是你必须完成web part的导出工作。你必须把这个“可以导出(enableExport)设置”添加到你的应用程序配置文件里。下面Listing 12就是Web.Config文件包含的属性设置了。

Listing 12. Web.Config

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    <system.web>
        <webParts enableExport="true" />
    </system.web>
</configuration>

那么修改一下主页程序,就有了下面的Listing 13,可以用来放置可导出的web part。

Listing 13. Home5.aspx (C#)

<%@ Page Language="C#" %>
<%@ Register TagPrefix="custom" Namespace="myControls" %>
<script runat="server">
   
    void CustomizePage(Object s, EventArgs e)
    {
        WebPartManager1.DisplayMode =
          WebPartManager.CatalogDisplayMode;
    }
    void EditWebParts(Object s, EventArgs e)
    {
        WebPartManager1.DisplayMode = WebPartManager.EditDisplayMode;
    }
           
</script>
<html>
<head id="Head1" runat="server">
    <title>Home Page</title>
</head>
<body bgcolor="gray">
    <form id="form1" runat="server">
    <asp:WebPartManager
        ID="WebPartManager1"
        Runat="Server" />
   
    <table width="100%" Height="100%" cellpadding="5" cellspacing="10">
    <tr height="50">
        <td colspan="3" align="right" bgcolor="white">
        <asp:LinkButton ID="LinkButton1"
            Text="Customize Page"
            OnClick="CustomizePage"
            Runat="Server" />
        |
        <asp:LinkButton ID="LinkButton2"
            Text="Edit Web Parts"
            OnClick="EditWebParts"
            Runat="Server" />
        </td>
    </tr>
    <tr>
        <td valign="top" bgcolor="white" width="40%">
        <asp:WebPartZone
            ID="WebPartZone1"
            Width="100%"
            HeaderStyle-BackColor="lightblue"
            PartTitleStyle-BackColor="silver"
            MenuStyle-BackColor="#eeeeee"
            MenuStyle-BorderStyle="solid"
            MenuStyle-BorderWidth="1"
            MenuStyle-BorderColor="black"
            Runat="Server" />
        </td>
        <td valign="top" bgcolor="white" width="40%">   
        <asp:WebPartZone
            ID="WebPartZone2"
            Width="100%"
            HeaderStyle-BackColor="lightblue"
            PartTitleStyle-BackColor="silver"
            MenuStyle-BackColor="#eeeeee"
            MenuStyle-BorderStyle="solid"
            MenuStyle-BorderWidth="1"
            MenuStyle-BorderColor="black"
            Runat="Server" />
        </td>
        <td valign="top" width="20%" bgcolor="white">
            <asp:CatalogZone
                ID="CatalogZone1" 
                Runat="server">
            <ZoneTemplate>
                <asp:DeclarativeCatalogPart
                    ID="DeclarativeCatalogPart1"
                    Runat="server">
                  <WebPartsTemplate>
                    <custom:FeaturedProductPart5
                        ID="myFeaturedProduct"
                        Runat="Server" />
                  </WebPartsTemplate>
                </asp:DeclarativeCatalogPart>
                <asp:ImportCatalogPart
                    ID="ImportCatalogPart1"
                    Runat="server" />
            </ZoneTemplate>
            </asp:CatalogZone>
           
            <asp:EditorZone
                ID="EditorZone1"
                Runat="Server">
                <ZoneTemplate>
                <asp:PropertyGridEditorPart
                    id="PropertyGridEditorPart1"
                    Runat="Server" />
                </ZoneTemplate>
            </asp:EditorZone>
 
        </td>
    </tr>   
    </table>
    </form>
</body>
</html>

Listing 13. Home5.aspx (Visual Basic .NET)

<%@ Page Language="vb" %>
<%@ Register TagPrefix="custom" Namespace="myControls" %>
<script runat="server">
   
    Sub CustomizePage(ByVal s As Object, ByVal e As EventArgs)
        WebPartManager1.DisplayMode = WebPartManager.CatalogDisplayMode
    End Sub
    Sub EditWebParts(ByVal s As Object, ByVal e As EventArgs)
        WebPartManager1.DisplayMode = WebPartManager.EditDisplayMode
    End Sub
           
</script>
<html>
<head id="Head1" runat="server">
    <title>Home Page</title>
</head>
<body bgcolor="gray">
    <form id="form1" runat="server">
    <asp:WebPartManager
        ID="WebPartManager1"
        Runat="Server" />
   
    <table width="100%" Height="100%" cellpadding="5" cellspacing="10">
    <tr height="50">
        <td colspan="3" align="right" bgcolor="white">
        <asp:LinkButton ID="LinkButton1"
            Text="Customize Page"
            OnClick="CustomizePage"
            Runat="Server" />
        |
        <asp:LinkButton ID="LinkButton2"
            Text="Edit Web Parts"
            OnClick="EditWebParts"
            Runat="Server" />
        </td>
    </tr>
    <tr>
        <td valign="top" bgcolor="white" width="40%">
        <asp:WebPartZone
            ID="WebPartZone1"
            Width="100%"
            HeaderStyle-BackColor="lightblue"
            PartTitleStyle-BackColor="silver"
            MenuStyle-BackColor="#eeeeee"
            MenuStyle-BorderStyle="solid"
            MenuStyle-BorderWidth="1"
            MenuStyle-BorderColor="black"
            Runat="Server" />
        </td>
        <td valign="top" bgcolor="white" width="40%">   
        <asp:WebPartZone
            ID="WebPartZone2"
            Width="100%"
            HeaderStyle-BackColor="lightblue"
            PartTitleStyle-BackColor="silver"
            MenuStyle-BackColor="#eeeeee"
            MenuStyle-BorderStyle="solid"
            MenuStyle-BorderWidth="1"
            MenuStyle-BorderColor="black"
            Runat="Server" />
        </td>
        <td valign="top" width="20%" bgcolor="white">
            <asp:CatalogZone
                ID="CatalogZone1" 
                Runat="server">
            <ZoneTemplate>
                <asp:DeclarativeCatalogPart
                    ID="DeclarativeCatalogPart1"
                    Runat="server">
                  <WebPartsTemplate>
                    <custom:FeaturedProductPart5
                        ID="myFeaturedProduct"
                        Runat="Server" />
                  </WebPartsTemplate>
                </asp:DeclarativeCatalogPart>
                <asp:ImportCatalogPart
                    ID="ImportCatalogPart1"
                    Runat="server" />
            </ZoneTemplate>
            </asp:CatalogZone>
           
            <asp:EditorZone
                ID="EditorZone1"
                Runat="Server">
                <ZoneTemplate>
                <asp:PropertyGridEditorPart
                    id="PropertyGridEditorPart1"
                    Runat="Server" />
                </ZoneTemplate>
            </asp:EditorZone>
 
        </td>
    </tr>   
    </table>
    </form>
</body>
</html>

当你打开上面的那个页面时,你可以点“自定义页(Customize Page)”,并且加上一个或多个Featured Product web part到页面中的Web Zones区域。在你添加一个web part以后,你可以通过选择web part菜单里导出命令来导出它的设置(见图7)。

Figure 7. Exporting a Web Part

当你导出一个web part时,一个名为FeaturedProduct.WebPart的文件就被创建了,这个文件你可以保存到本地硬盘上,它所包含的内容都列在Listing 14中。注意这个文件是一个XML文件,简单地罗列了web part属性值,其中包括CategoryName这个属性。

Listing 14. FeaturedProduct.WebPart

<?xml version="1.0" encoding="utf-8"?>
<webParts>
  <webPart>
    <metaData>
      <type name="myControls.FeaturedProductPart5" />
      <importErrorMessage>Cannot import this Web
       Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="TitleIconImageUrl" />
        <property name="AllowClose">True</property>
        <property name="ChromeState">Normal</property>
        <property name="CategoryName">Beverages</property>
        <property name="TitleUrl" />
        <property name="AllowZoneChange">True</property>
        <property name="Hidden">False</property>
        <property name="Direction">NotSet</property>
        <property name="HelpMode">Navigate</property>
        <property name="ChromeType">Default</property>
        <property name="CatalogIconImageUrl" />
        <property name="Height" />
        <property name="Description" />
        <property name="AllowHide">True</property>
        <property name="AllowMinimize">True</property>
        <property name="AllowEdit">True</property>
        <property name="Title">Featured Product</property>
        <property name="Width" />
        <property name="ExportMode">All</property>
        <property name="HelpUrl" />
      </properties>
    </data>
  </webPart>
</webParts>

你导出一个web part以后,你可以通过点“自定义页面”来导入这个web part。其中一个显示出的分类(catalogs)就叫做导入web part分类。如果你选这个分类,你可以上传你先前导出的那个web part(见图8)。

Figure 8. Importing a Web Part

posted on 2005-08-23 10:38  YuL  阅读(1961)  评论(5编辑  收藏  举报