Common Custom controls
Common_CustomControls_CSharp\CustomControlsWebSite\AddWebPartOnClick.aspx.cs
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 


public partial class AddWebPartOnClick : System.Web.UI.Page
{
    // Initializes a MapWebPart and adds it to the page when the Add MapWebPart button is clicked
    protected void btnAddMapWebPart_Click(object sender, System.EventArgs e)
    {
        ADFWebPart.MapWebPart mapWebPart = new ADFWebPart.MapWebPart();
        mapWebPart.ID = System.Guid.NewGuid().ToString();
        mapWebPart.DataSourceType = "ArcGIS Server Internet";
        mapWebPart.DataSource = "http://serverapps.esri.com/arcgis/services";
        mapWebPart.ResourceDefinition = "Layers@SamplesNET/NorthAmerica";

        WebPartManager1.AddWebPart(mapWebPart, WebPartZone1, 0);
    }

    // Initializes a MapGridViewWebPart and adds it to the page when the Add MapGridViewWebPart button is clicked
    protected void btnAddMapGridViewWebPart_Click(object sender, System.EventArgs e)
    {
        ADFWebPart.MapGridViewWebPart mapGridViewWebPart = new ADFWebPart.MapGridViewWebPart();
        mapGridViewWebPart.ID = System.Guid.NewGuid().ToString();
        mapGridViewWebPart.DataSourceType = "ArcGIS Server Internet";
        mapGridViewWebPart.DataSource = "http://serverapps.esri.com/arcgis/services/SamplesNET";
        // SanFrancisco.mxd in <ArcGIS Developer Kit Install>\Samples\data\SanFrancisco
        mapGridViewWebPart.ResourceDefinition = "1@Layers@SanFrancisco";
        mapGridViewWebPart.DataLayerName = "customers";
        mapGridViewWebPart.GeometryServiceUrl = "http://serverapps.esri.com/arcgis/services/Geometry/GeometryServer";

        // Initialize a NameValueCollection to specify how we want fields to be displayed.  This specifies the
        // field display for both the WebPart's GridView and callouts when features are hovered over.
        System.Collections.Specialized.NameValueCollection fieldsCollection =
            new System.Collections.Specialized.NameValueCollection();
        fieldsCollection.Add("NAME", "Site Name");
        fieldsCollection.Add("ADDRESS", "Address");
        fieldsCollection.Add("SALES", "Sales");

        mapGridViewWebPart.DisplayFields = fieldsCollection;

        WebPartManager1.AddWebPart(mapGridViewWebPart, WebPartZone1, 0);
    }

    // Removes all Webparts from the page
    protected void btnRemoveWebParts_Click(object sender, System.EventArgs e)
    {
        foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in WebPartManager1.WebParts)
        {
            WebPartManager1.DeleteWebPart(webPart);
        }
    }
}