WebPartManager Web Server Control Declarative Syntax 

Serves as the central class of the Web Parts control set, managing all the Web Parts controls, functionality, and events that occur on a Web page.

<asp:WebPartManager
    CloseProviderWarning="string"
    DeleteWarning="string"
    EnableClientScript="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    ExportSensitiveDataWarning="string"
    ID="string"
    OnAuthorizeWebPart="AuthorizeWebPart event handler"
    OnConnectionsActivated="ConnectionsActivated event handler"
    OnConnectionsActivating="ConnectionsActivating event handler"
    OnDataBinding="DataBinding event handler"
    OnDisplayModeChanged="DisplayModeChanged event handler"
    OnDisplayModeChanging="DisplayModeChanging event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnSelectedWebPartChanged="SelectedWebPartChanged event handler"
    OnSelectedWebPartChanging="SelectedWebPartChanging event handler"
    OnUnload="Unload event handler"
    OnWebPartAdded="WebPartAdded event handler"
    OnWebPartAdding="WebPartAdding event handler"
    OnWebPartClosed="WebPartClosed event handler"
    OnWebPartClosing="WebPartClosing event handler"
    OnWebPartDeleted="WebPartDeleted event handler"
    OnWebPartDeleting="WebPartDeleting event handler"
    OnWebPartMoved="WebPartMoved event handler"
    OnWebPartMoving="WebPartMoving event handler"
    OnWebPartsConnected="WebPartsConnected event handler"
    OnWebPartsConnecting="WebPartsConnecting event handler"
    OnWebPartsDisconnected="WebPartsDisconnected event handler"
    OnWebPartsDisconnecting="WebPartsDisconnecting event handler"
    Personalization-Enabled="True|False"
    Personalization-InitialScope="User|Shared"
    Personalization-ProviderName="string"
    runat="server"
    SkinID="string"
>
    <Personalization
        Enabled="True|False"
        InitialScope="User|Shared"
        ProviderName="string" />
    <StaticConnections>
        <asp:WebPartConnection
            ConsumerConnectionPointID="string"
            ConsumerID="string"
            ID="string"
            ProviderConnectionPointID="string"
            ProviderID="string" >
            <asp:RowToFieldTransformer
                FieldName="string" />
            <asp:RowToParametersTransformer
                ConsumerFieldNames="string"
                ProviderFieldNames="string"/>
        </asp:WebPartConnection>
    </StaticConnections>
</asp:WebPartManager>

Remarks

The WebPartManager control acts as the hub or control center of a Web Parts application. There must be one—and only one—instance of the WebPartManager control on each page that uses Web Parts controls and it must be placed before any zone controls are placed. As with most aspects of Web Parts applications, the WebPartManager control works only with authenticated users. Further, its functionality works almost entirely with server controls that reside within Web Parts zones that inherit from the WebZone class. Server controls that reside on a page outside of these zones can have very little Web Parts functionality or interaction with the WebPartManager control.

For more information on the WebPartManager and Web Parts controls, see ASP.NET Web Parts Pages.

Example

The following code example demonstrates how to use the WebPartManager control declaratively in a Web Parts scenario using the Calendar control. The Web page allows a user to enter edit mode to edit certain aspects of the Calendar control. The Toggle Scope button switches the page to user or shared personalization scope. The Edit Mode and Browse Mode buttons each switch the page into the appropriate display mode. Notice that in the <script> tag section of the file, two of the methods that handle events use the Personalization property to access useful members of the underlying object. Specifically, these methods use the ToggleScope method and the Scope property on the object that is accessed through the Personalization property.

For the code example to run, you also must enable one or more users to personalize pages in shared scope. To do this, add an entry to the Web.config file, in the webParts Element (ASP.NET Settings Schema).

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If ((mgr1.Personalization.Scope = PersonalizationScope.User) _
           And (mgr1.Personalization.CanEnterSharedScope)) Then
            mgr1.Personalization.ToggleScope()
    
        ElseIf (mgr1.Personalization.Scope = PersonalizationScope.Shared) Then
            mgr1.Personalization.ToggleScope()
        Else
            'If the user cannot enter shared scope you may want
            ' to notify them on the page.
        End If
    End Sub
    

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        mgr1.DisplayMode = WebPartManager.EditDisplayMode
    End Sub    

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        mgr1.DisplayMode = WebPartManager.BrowseDisplayMode
    End Sub
    

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As CommandEventArgs)
        Label1.Text = "Scope is: " _
        + mgr1.Personalization.Scope.ToString()
    End Sub

</script>


<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="head1" runat="server">
    <title>Web Parts Declarative Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:LoginName id="LoginName1" runat="server" />
      <asp:WebPartManager id="mgr1" runat="server">
        <Personalization InitialScope="Shared" Enabled="True" />
      </asp:WebPartManager>
      <asp:WebPartZone id="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:Calendar id="Calendar1" runat="server" />
        </ZoneTemplate>
        <CloseVerb Text="Close This Part" />
        <MinimizeVerb Text="Minimize This Part" />
        <EditVerb Text="Edit This Part" />
      </asp:WebPartZone>
      <asp:EditorZone id="EditorZone1" runat="server">
        <ZoneTemplate>
          <asp:LayoutEditorPart HorizontalAlign="Center" id="Layout1" runat="server" Title="My Custom Layout" />
          <asp:AppearanceEditorPart id="AppearanceEditorPart1" 
            runat="server" BackColor="LightGray" />
          <asp:BehaviorEditorPart id="BehaviorEditorPart1" 
            runat="server"  />
        </ZoneTemplate>
      </asp:EditorZone>
      <hr />
      <asp:Button id="Button1" runat="server" Text="Toggle Scope" OnClick="Button1_Click"  />
      <asp:Button id="Button2" runat="server" Text="Edit Mode" OnClick="Button2_Click" />
      <asp:Button id="Button3" runat="server" Text="Browse Mode" OnClick="Button3_Click" />
      <br />
      <asp:Label id="Label1" runat="server" Text="" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="C#" %>

<!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 Button1_Click(object sender, EventArgs e)
  {
    if ((mgr1.Personalization.Scope == PersonalizationScope.User)
        && (mgr1.Personalization.CanEnterSharedScope))
    {
      mgr1.Personalization.ToggleScope();
    }
    else if (mgr1.Personalization.Scope ==
          PersonalizationScope.Shared)
    {
      mgr1.Personalization.ToggleScope();
    }
    else
    {
      // If the user cannot enter shared scope you may want
      // to notify them on the page.
    }
  }
    
    

  protected void Button2_Click(object sender, EventArgs e)
  {
    mgr1.DisplayMode = WebPartManager.EditDisplayMode;
  }
    

  protected void Button3_Click(object sender, EventArgs e)
  {
    mgr1.DisplayMode = WebPartManager.BrowseDisplayMode;
  }

    
  protected void Page_Load(object sender, EventArgs e)
  {
    Label1.Text = "Scope is: "
      + mgr1.Personalization.Scope.ToString();
  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="head1" runat="server">
    <title>Web Parts Declarative Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:LoginName id="LoginName1" runat="server" />
      <asp:WebPartManager id="mgr1" runat="server">
        <Personalization InitialScope="Shared" Enabled="True" />
      </asp:WebPartManager>
      <asp:WebPartZone id="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:Calendar id="Calendar1" runat="server" />
        </ZoneTemplate>
        <CloseVerb Text="Close This Part" />
        <MinimizeVerb Text="Minimize This Part" />
        <EditVerb Text="Edit This Part" />
      </asp:WebPartZone>
      <asp:EditorZone id="EditorZone1" runat="server">
        <ZoneTemplate>
          <asp:LayoutEditorPart HorizontalAlign="Center" id="Layout1" runat="server" Title="My Custom Layout" />
          <asp:AppearanceEditorPart id="AppearanceEditorPart1" 
            runat="server" BackColor="LightGray" />
          <asp:BehaviorEditorPart id="BehaviorEditorPart1" 
            runat="server"  />
        </ZoneTemplate>
      </asp:EditorZone>
      <hr />
      <asp:Button id="Button1" runat="server" Text="Toggle Scope" OnClick="Button1_Click"  />
      <asp:Button id="Button2" runat="server" Text="Edit Mode" OnClick="Button2_Click" />
      <asp:Button id="Button3" runat="server" Text="Browse Mode" OnClick="Button3_Click" />
      <br />
      <asp:Label id="Label1" runat="server" Text="" />
    </div>
    </form>
</body>
</html>

See Also

Reference

WebPartManager

Other Resources

ASP.NET Web Parts Pages