Edit

Share via


WebPartManager.MoveWebPart(WebPart, WebPartZoneBase, Int32) Method

Definition

Moves a WebPart or server control from one WebPartZoneBase zone to another, or to a new position within the same zone.

public:
 virtual void MoveWebPart(System::Web::UI::WebControls::WebParts::WebPart ^ webPart, System::Web::UI::WebControls::WebParts::WebPartZoneBase ^ zone, int zoneIndex);
public virtual void MoveWebPart (System.Web.UI.WebControls.WebParts.WebPart webPart, System.Web.UI.WebControls.WebParts.WebPartZoneBase zone, int zoneIndex);
abstract member MoveWebPart : System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.WebPartZoneBase * int -> unit
override this.MoveWebPart : System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.WebPartZoneBase * int -> unit
Public Overridable Sub MoveWebPart (webPart As WebPart, zone As WebPartZoneBase, zoneIndex As Integer)

Parameters

webPart
WebPart

The WebPart or other server control being moved.

zone
WebPartZoneBase

The target WebPartZoneBase to which webPart is being moved.

zoneIndex
Int32

An integer that indicates the index of webPart relative to other controls within zone.

Exceptions

webPart is not contained in the Controls collection of the WebPartManager control.

-or-

zone is not contained in the Zones collection of the WebPartManager control.

-or-

The zone referenced by the webPart control's Zone property is null, which means that webPart is not currently contained in a zone.

webPart or zone is null.

zoneIndex is less than zero.

Examples

The following code example demonstrates how to call the MoveWebPart method directly from code to move a WebPart control from one zone to another.

The code example has three parts:

  • A user control for changing display modes.

  • A Web page to host the controls.

  • An explanation of how to run the code example.

The first part of the code example is the user control for changing display modes. You can obtain the source code for the user control from the Example section of the WebPartManager class overview. For more information about display modes and how the user control works, see Walkthrough: Changing Display Modes on a Web Parts Page.

The second part of the example is a Web page that contains two zones, each of which contains two server controls. When a user clicks the Move WebPart button on the page, the code in the Button1_Click method moves a control from the first zone to a new position in the second zone. Notice that the code first must call the GetGenericWebPart method to retrieve the GenericWebPart object that wraps the list1 control. This is required because the first parameter of the MoveWebPart method requires a WebPart control, whereas list1 is an ASP.NET server control.

<%@ Page Language="C#" %>
<%@ Register TagPrefix="uc1" 
    TagName="DisplayModeMenuCS"
    Src="~/displaymodemenucs.ascx" %>

<!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)
  {
    GenericWebPart part = mgr1.GetGenericWebPart(list1);
    mgr1.MoveWebPart(part, zone2, zone2.WebParts.Count - 1);
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr1" runat="server" />
      <uc1:DisplayModeMenuCS ID="menu1" runat="server" />
      <asp:WebPartZone ID="zone1" runat="server">
        <ZoneTemplate>
          <asp:Label ID="Label1" runat="server" 
            Text="My Navigation" 
            Title="Zone 1 Label"/>
          <asp:BulletedList 
            ID="list1" 
            Runat="server"
            DisplayMode="HyperLink" 
            Title="Favorite Links"
            AuthorizationFilter="admin">
            <asp:ListItem Value="http://msdn.microsoft.com">
              MSDN
            </asp:ListItem>
            <asp:ListItem Value="http://www.asp.net">
              ASP.NET
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
              MSN
            </asp:ListItem>
          </asp:BulletedList>        
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:WebPartZone ID="zone2" runat="server">
        <ZoneTemplate>
          <asp:Label ID="Label2" runat="server" 
            Text="My Data" 
            Title="Zone 2 Label"/>
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar"/>
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Button ID="Button1" runat="server" 
        Text="Move WebPart" 
        OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ Register TagPrefix="uc1" 
    TagName="DisplayModeMenuVB"
    Src="~/displaymodemenuvb.ascx" %>

<!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)

    Dim part As GenericWebPart = mgr1.GetGenericWebPart(list1)
    mgr1.MoveWebPart(part, zone2, zone2.WebParts.Count - 1)
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr1" runat="server" />
      <uc1:DisplayModeMenuVB ID="menu1" runat="server" />
      <asp:WebPartZone ID="zone1" runat="server">
        <ZoneTemplate>
          <asp:Label ID="Label1" runat="server" 
            Text="My Navigation" 
            Title="Zone 1 Label"/>
          <asp:BulletedList 
            ID="list1" 
            Runat="server"
            DisplayMode="HyperLink" 
            Title="Favorite Links"
            AuthorizationFilter="admin">
            <asp:ListItem Value="http://msdn.microsoft.com">
              MSDN
            </asp:ListItem>
            <asp:ListItem Value="http://www.asp.net">
              ASP.NET
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
              MSN
            </asp:ListItem>
          </asp:BulletedList>        
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:WebPartZone ID="zone2" runat="server">
        <ZoneTemplate>
          <asp:Label ID="Label2" runat="server" 
            Text="My Data" 
            Title="Zone 2 Label"/>
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar"/>
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Button ID="Button1" runat="server" 
        Text="Move WebPart" 
        OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

After the page loads, click the Move WebPart button, and note that the control that contains the links moves into the middle position in the second zone. This movement of the control is accomplished programmatically by calling the MoveWebPart method. You can also use the Display Mode drop-down list control to switch the page to design mode, and in design mode, you can drag the controls to different zones or different positions within their zones. The MoveWebPart method is also called by the WebPartManager control to handle such user-initiated moves.

Remarks

The WebPartManager control uses the MoveWebPart method to move webPart to a new location in the same zone, or in a different zone. You can call this method directly from code, and it is also invoked when a user moves a control to a new position using various options in the Web Parts user interface (UI).

A number of conditions must be met before webPart can be moved, and most of these are indicated by items listed in the Exceptions section of this topic. If webPart was not contained in a WebPartZoneBase zone to begin with, it cannot be moved into a zone.

After the conditions have been met, the following sequence of actions occurs to move webPart:

  1. The OnWebPartMoving method raises the WebPartMoving event.

  2. webPart is removed from its current zone (if necessary), and added to its new zone or new position within its current zone.

  3. The OnWebPartMoved method raises the WebPartMoved event.

  4. The zoneIndex of every WebPart control in both the origin and destination zones is reset to reflect the moved control.

Applies to

See also