This documentation is archived and is not being maintained.

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


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

The WebPart or other server control being moved.

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

The target WebPartZoneBase to which webPart is being moved.

Type: System.Int32

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


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.

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" 
    Src="~/displaymodemenucs.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<script runat="server">

  protected void Button1_Click(object sender, EventArgs e)
    GenericWebPart part = mgr1.GetGenericWebPart(list1);
    mgr1.MoveWebPart(part, zone2, zone2.WebParts.Count - 1);

<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
      <asp:WebPartManager ID="mgr1" runat="server" />
      <uc1:DisplayModeMenuCS ID="menu1" runat="server" />
      <asp:WebPartZone ID="zone1" runat="server">
          <asp:Label ID="Label1" runat="server" 
            Text="My Navigation" 
            Title="Zone 1 Label"/>
            Title="Favorite Links"
            <asp:ListItem Value="">
            <asp:ListItem Value="">
            <asp:ListItem Value="">
      <asp:WebPartZone ID="zone2" runat="server">
          <asp:Label ID="Label2" runat="server" 
            Text="My Data" 
            Title="Zone 2 Label"/>
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar"/>
      <hr />
      <asp:Button ID="Button1" runat="server" 
        Text="Move WebPart" 
        OnClick="Button1_Click" />

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.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0