Export (0) Print
Expand All
Expand Minimize

WebPartManager.MoveWebPart Method

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

Namespace:  System.Web.UI.WebControls.WebParts
Assembly:  System.Web (in System.Web.dll)

public virtual void MoveWebPart(
	WebPart webPart,
	WebPartZoneBase zone,
	int zoneIndex
)

Parameters

webPart
Type: System.Web.UI.WebControls.WebParts.WebPart

The WebPart or other server control being moved.

zone
Type: System.Web.UI.WebControls.WebParts.WebPartZoneBase

The target WebPartZoneBase to which webPart is being moved.

zoneIndex
Type: System.Int32

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

ExceptionCondition
ArgumentException

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.

ArgumentNullException

webPart or zone is null.

ArgumentOutOfRangeException

zoneIndex is less than zero.

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.

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>

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.

.NET Framework

Supported in: 4.6, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft