ASP.NET - BehaviorEditorPart Web 服务器控件概述

      BehaviorEditorPart Web 服务器控件是一个编辑器部件控件,最终用户可以使用该控件在运行时更改关联的 WebPart 控件上的多个用户界面 (UI) 属性。下表列出了用户可自定义的功能,以及用户可使用 BehaviorEditorPart 控件编辑的对应的 WebPart 控件属性值。

用户可自定义的功能

WebPart 控件属性

设置 Web 部件的说明文本。

·                       Description

设置标题链接。

·                       TitleUrl

设置标题图标图像链接。

·                       TitleIconImageUrl

设置目录图标图像链接。

·                       CatalogIconImageUrl

设置帮助链接。

·                       HelpUrl

隐藏或显示控件。

Hidden

指定帮助模式。

·                       HelpMode

选择导出模式。

·                       ExportMode

设置授权筛选器。

·                       AuthorizationFilter

指定是否可从页移除 Web 部件。

·                       AllowClose

指定 Web 部件的属性是否可编辑。

·                       AllowEdit

指定 Web 部件是否可隐藏。

·                       AllowHide

指定 Web 部件是否可最小化。

·                       AllowMinimize

指定 Web 部件是可以在区域间移动,还是只可以在其自己的区域内移动。

·                       AllowZoneChange

 

启用 BehaviorEditorPart Web 服务器控件

如果 Web 部件页处于编辑模式且用户已选择关联的 WebPart 控件进行编辑,BehaviorEditorPart 控件会在运行时显示。BehaviorEditorPart 控件驻留在 EditorZone 控件中。

说明: EditorZone 区域只能包含 EditorPart 控件。EditorPart 控件不能放在任何其他类型的区域中。

 

当满足以下条件时,BehaviorEditorPart 控件不会在页上显示:

·将要编辑的 WebPart 控件的 IsShared 属性设置为 true.

·页在用户个性化设置范围中(默认)。

 

对于要显示的 BehaviorEditorPart 控件,必须执行以下操作:

·向 Web.config 文件中添加项,该项为授权用户授予在共享的个性化设置范围内更改网页的权限。这意味着该用户所做的更改对所有其他用户都可见。

在运行时,以编程方式将页从用户级更改为共享级个性化设置范围。例如,可以通过调用 ToggleScope 方法来更改范围。

 

一、BehaviorEditorPart

      提供一个编辑器控件,最终用户使用该控件可以编辑关联 WebPart 控件的几个用户界面 (UI) 属性。无法继承此类。

      BehaviorEditorPart 类是从 EditorPart 类派生的编辑器控件,用于编辑会影响关联 WebPart GenericWebPart 控件的行为的属性。

正如 Web 部件控件集中有面向工具的区域一样(有关更多信息,请参见 ToolZone 类概述),其中也有面向工具的部件控件(工具部件),并且每个工具部件都必须位于某种特定类型的工具区域中。Web 部件控件集中的工具部件具有两个显著特征:

·它们是帮助器控件,最终用户可用来个性化 Web 部件页上的控件。

·它们只在某些显示模式下可见。

 

BehaviorEditorPart 控件是一个工具部件,仅当 Web 部件页处于编辑模式,并选择一个特定 WebPart 控件进行编辑时,才会变得可见。BehaviorEditorPart 控件与所有其他 EditorPart 控件一样,位于一个 EditorZone 控件中。

      重要说明: EditorZone 区域只能包含 EditorPart 控件,且 EditorPart 控件不能放在任何其他类型的区域中。

 

在某些条件下,该控件不会在页上显示。如果正在编辑的 WebPart 控件的 IsShared 属性值为 true,并且页处于用户个性化设置范围内(默认值),则不显示该控件。在这种情况下,对于要显示的 BehaviorEditorPart 控件,通常需要执行两个步骤:

·向 Web.config 文件添加项,该项提供授权用户权限,以便编辑共享个性化设置范围内的网页(意味着用户所做的更改对应用程序的所有用户都可见)。

·在运行时以编程方式将页从用户级别更改为共享级别个性化设置范围,例如,通过使用 ToggleScope 方法。

 

BehaviorEditorPart 类还有一个重要方法 SyncChanges,该方法是从 EditorPart 类继承并重写的。此方法至关重要,因为使用它可从它正在编辑的 WebPart 控件获取属性值,并将这些值分配给正在编辑控件的字段。

 

在大部分情况下,BehaviorEditorPart 控件应在页持久性格式中使用,方法是在 <zonetemplate> 元素中声明一个 <asp:behavioreditorpart> 元素,前一个元素又包含在网页上的 <asp:editorzone> 元素中。最终用户使用 BehaviorEditorPart 控件可以编辑 WebPart 控件的下列用户界面属性:

DescriptionTitleUrlTitleIconImageUrlCatalogIconImageUrlHelpUrl

HelpModeImportErrorMessageExportModeAuthorizationFilter

AllowCloseAllowConnectAllowEditAllowHideAllowMinimize

AllowZoneChange

 

若要编辑 WebPart 控件的其他属性和行为,可使用 Web 部件控件集提供的其他 EditorPart 控件。这些控件包括 LayoutEditorPartAppearanceEditorPart PropertyGridEditorPart 控件。提供的 EditorPart 控件应提供编辑 WebPart 控件所需的大部分编辑功能,但通过从 EditorPart 类继承还是可以创建自定义编辑器控件。

说明: 为改进可访问性,在 <fieldset> 元素中呈现了 BehaviorEditorPart 控件。<fieldset> 元素在 BehaviorEditorPart 控件中对一组用于编辑的相关控件进行分组,还为在那些兼用于可视化用户代理(如普通 Web 浏览器)和面向语音的用户代理(如屏幕阅读软件)的控件之间进行选项卡式导航提供便利。

 

1.1、示例

下面的代码示例演示如何在网页中声明一个 BehaviorEditorPart 控件,并使它能够编辑 WebPart 控件的几个用户界面属性。该代码示例包含以下四部分:

·一个用户控件,可用于更改 Web 部件页上的显示模式。

·一个网页,包含一个 EditorZone 控件和一个用于编辑 BulletedList 控件的行为属性的 BehaviorEditorPart 控件。

·添加 Web.config 文件中的项,它用来提供授权用户权限,以便编辑共享个性化设置范围内的页。

·对您在浏览器中加载页面时示例的运行方式的说明。

 

此代码示例的第一部分是一个用户控件,该控件使用户能够更改网页上的显示模式。此用户控件的文档说明了如何调用 ToggleScope 方法将页切换到共享模式,这种模式中,即会显示 BehaviorEditorPart 控件。

<%@ control language="C#" classname="DisplayModeMenuCS"%>

<script runat="server">

 

 // Use a field to reference the current WebPartManager.

  WebPartManager _manager;

 

  void Page_Init(object sender, EventArgs e)

  {

    Page.InitComplete += new EventHandler(InitComplete);

  } 

 

  void InitComplete(object sender, System.EventArgs e)

  {

    _manager = WebPartManager.GetCurrentWebPartManager(Page);

 

    String browseModeName = WebPartManager.BrowseDisplayMode.Name;

 

    // Fill the dropdown with the names of supported display modes.

    foreach (WebPartDisplayMode mode in _manager.SupportedDisplayModes)

    {

      String modeName = mode.Name;

      // Make sure a mode is enabled before adding it.

      if (mode.IsEnabled(_manager))

      {

        ListItem item = new ListItem(modeName, modeName);

        DisplayModeDropdown.Items.Add(item);

      }

    }

 

    // If shared scope is allowed for this user, display the scope-switching

    // UI and select the appropriate radio button for the current user scope.

    if (_manager.Personalization.CanEnterSharedScope)

    {

      Panel2.Visible = true;

      if (_manager.Personalization.Scope == PersonalizationScope.User)

        RadioButton1.Checked = true;

      else

        RadioButton2.Checked = true;

    }

 

  }

 

  // Change the page to the selected display mode.

  void DisplayModeDropdown_SelectedIndexChanged(object sender, EventArgs e)

  {

    String selectedMode = DisplayModeDropdown.SelectedValue;

 

    WebPartDisplayMode mode = _manager.SupportedDisplayModes[selectedMode];

    if (mode != null)

      _manager.DisplayMode = mode;

  }

 

  // Set the selected item equal to the current display mode.

  void Page_PreRender(object sender, EventArgs e)

  {

    ListItemCollection items = DisplayModeDropdown.Items;

    int selectedIndex =

      items.IndexOf(items.FindByText(_manager.DisplayMode.Name));

    DisplayModeDropdown.SelectedIndex = selectedIndex;

  }

 

  // Reset all of a user's personalization data for the page.

  protected void LinkButton1_Click(object sender, EventArgs e)

  {

    _manager.Personalization.ResetPersonalizationState();

  }

 

  // If not in User personalization scope, toggle into it.

  protected void RadioButton1_CheckedChanged(object sender, EventArgs e)

  {

    if (_manager.Personalization.Scope == PersonalizationScope.Shared)

      _manager.Personalization.ToggleScope();

  }

 

  // If not in Shared scope, and if user is allowed, toggle the scope.

  protected void RadioButton2_CheckedChanged(object sender, EventArgs e)

  {

    if (_manager.Personalization.CanEnterSharedScope &&

        _manager.Personalization.Scope == PersonalizationScope.User)

      _manager.Personalization.ToggleScope();

  }

</script>

<div>

  <asp:Panel ID="Panel1" runat="server"

    Borderwidth="1"

    Width="230"

    BackColor="lightgray"

    Font-Names="Verdana, Arial, Sans Serif" >

    <asp:Label ID="Label1" runat="server"

      Text="&nbsp;Display Mode"

      Font-Bold="true"

      Font-Size="8"

      Width="120"

      AssociatedControlID="DisplayModeDropdown"/>

    <asp:DropDownList ID="DisplayModeDropdown" runat="server" 

      AutoPostBack="true"

      Width="120"

      OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" />

    <asp:LinkButton ID="LinkButton1" runat="server"

      Text="Reset User State"

      ToolTip="Reset the current user's personalization data for the page."

      Font-Size="8"

      OnClick="LinkButton1_Click" />

    <asp:Panel ID="Panel2" runat="server"

      GroupingText="Personalization Scope"

      Font-Bold="true"

      Font-Size="8"

      Visible="false" >

      <asp:RadioButton ID="RadioButton1" runat="server"

        Text="User"

        AutoPostBack="true"

        GroupName="Scope" OnCheckedChanged="RadioButton1_CheckedChanged" />

      <asp:RadioButton ID="RadioButton2" runat="server"

        Text="Shared"

        AutoPostBack="true"

        GroupName="Scope"

        OnCheckedChanged="RadioButton2_CheckedChanged" />

    </asp:Panel>

  </asp:Panel>

</div>

 

      该代码示例的第二部分为网页。它包含一个对 EditorZone 控件的声明性引用,以及一个包含对 BehaviorEditorPart 控件的声明性引用的子 <zonetemplate> 元素。注意,该页还包含一个绑定到 Pubs 数据库的 BulletedList 控件;由于此控件位于 WebPartZone 中,因此可用作可由 BehaviorEditorPart 控件编辑的 WebPart 控件。

<%@ Page Language="C#" %>

<%@ Register Src="~/displayModeMenuCS.ascx"

  TagPrefix="uc1"

  TagName="DisplayModeMenuCS" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

 

  protected void Page_Load(object sender, EventArgs e)

  {

    Button1.Visible = false;

    TextBox1.Visible = false;

    BulletedList1.DataBind();

  }

 

  protected void Button1_Click(object sender, EventArgs e)

  {

    BehaviorEditorPart1.Title = Server.HtmlEncode(TextBox1.Text);

  }

 

  protected void BehaviorEditorPart1_PreRender(object sender,

    EventArgs e)

  {

    Button1.Visible = true;

    TextBox1.Visible = true;

  }

</script>

 

<html  >

<head runat="server">

    <title>ASP.NET Example</title>

</head>

<body>

  <form id="form1" runat="server">

    <!-- This example uses Microsoft SQL Server and connects    -->

    <!-- to the Pubs sample database. Use an ASP.NET expression -->

    <!-- like the one in the following control to retrieve the  -->

    <!-- connection string value from the Web.config file.      -->

    <asp:SqlDataSource ID="ds1" runat="server"

      connectionString="<%$ ConnectionStrings:PubsConnection %>"

      SelectCommand="Select au_id, au_lname, au_fname From Authors"/>

    <asp:WebPartManager ID="WebPartManager1" runat="server" />

    <uc1:DisplayModeMenuCS id="menu1" runat="server" />

    <asp:WebPartZone ID="WebPartZone1" runat="server" Width="150"

      style="z-index: 100; left: 10px; position: absolute; top: 90px" >

      <ZoneTemplate>

        <asp:Panel ID="panel1" runat="server" ToolTip="Author List WebPart">

          <asp:Label ID="Label1" runat="server"

            Text="Author Names"

            Font-Bold="true"

            Font-Size="120%"

            AssociatedControlID="BulletedList1"/>

          <asp:BulletedList ID="BulletedList1" runat="server"

            DataSourceID="ds1"

            DataTextField="au_lname"

            DataValueField="au_id"/>

        </asp:Panel>

      </ZoneTemplate>

    </asp:WebPartZone>

    <asp:WebPartZone ID="WebPartZone2" runat="server" Width="150"

      style="z-index: 101; left: 170px; position: absolute; top: 90px" />

    <asp:EditorZone ID="EditorZone1" runat="server"

      style="z-index: 102; left: 340px; position: absolute; top: 90px"

      Width="170px">

      <ZoneTemplate>

        <asp:BehaviorEditorPart ID="BehaviorEditorPart1" runat="server"

          Title="My BehaviorEditorPart" 

          OnPreRender="BehaviorEditorPart1_PreRender" />

      </ZoneTemplate>

    </asp:EditorZone>

    <asp:Button ID="Button1" runat="server" Width="140"

      Text="Update EditorPart Title"

      style="left: 340px; position: absolute; top: 65px; z-index: 103;"

      OnClick="Button1_Click" />

    <asp:TextBox ID="TextBox1" runat="server"

      style="z-index: 105; left: 500px; position: absolute; top: 65px" />

  </form>

</body>

</html>

此示例的第三部分是 Web.config 文件中的项。必须在文件的 <webParts> 节中添加类似下面的项,授权用户或用户集才能编辑处于共享个性化设置范围内的网页。该项是关键步骤;如果不添加,将页切换到编辑模式时,BehaviorEditorPart 控件将不可见。

<webParts>

<personalization>

<authorization>

<allow users="User_account" roles="admin"

verbs="enterSharedScope"/>

</authorization>

</personalization>

</webParts>

在浏览器中加载页时,可以首先在“显示模式”下拉列表控件上选择“共享”选项。下一步,单击下拉列表本身,然后选择“编辑模式”将页切换到编辑模式。您可以在“作者列表 Web 部件”控件的标题栏中单击谓词菜单(向下箭头),然后单击“编辑”开始编辑。如果编辑用户界面可见,则可以看到 BehaviorEditorPart 控件、一个按钮以及位于该按钮上方的文本框。如果在编辑用户界面中进行了一些更改并单击“应用”按钮,则可使用“显示模式”下拉列表控件使页返回到浏览模式,然后查看编辑更改的完整效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值