Common Custom EditorTask
Common_CustomEditorTask_CSharp\CustomEditorTask_CSharp\PolygonToLineLayerEditorPanel.cs
// Copyright 2011 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.
// 

namespace CustomEditorTask_CSharp
{
    class PolygonToLineLayerEditorPanel : ESRI.ArcGIS.ADF.ArcGISServer.Editor.EditorPanel
    {
        #region Instance Variable Declarations

        // Reference to the drop down list in the custom panel
        private System.Web.UI.WebControls.DropDownList m_layersDropDownList;

        #endregion

        #region Constructor

        public PolygonToLineLayerEditorPanel(string title, ESRI.ArcGIS.ADF.ArcGISServer.Editor.EditorTask editorTask, 
            string ID) : base(title, editorTask, ID) { }

        #endregion

        #region WebControl Life Cycle Event Overrides

        // Instantiates the custom panel's child controls
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // Label for the target layer drop-down list
            System.Web.UI.WebControls.Label targetLayerLabel = new System.Web.UI.WebControls.Label();
            targetLayerLabel.Text = "Target layer:  ";
            Controls.Add(targetLayerLabel);

            // Target layer drop-down list
            m_layersDropDownList = new System.Web.UI.WebControls.DropDownList();
            m_layersDropDownList.ID = "targetLayers";
            Controls.Add(m_layersDropDownList);

            // Call to add editable polyline layers to the drop down list
            AddPolylineLayers();
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            // When item selected (changed) in the drop down list, trigger a callback to this control.  
            // Process in GetCallbackResult()
            string jsOnChange = string.Format("javascript:var context;var argument='EventArg=targetChanged&layer=' " +
                "+ document.getElementById('{0}').value; eval(\"{1}\");", m_layersDropDownList.ClientID,
                CallbackFunctionString);
            m_layersDropDownList.Attributes.Add("onchange", jsOnChange);

            base.Render(writer);
        }

        #endregion

        #region Callback Handler

        public override string GetCallbackResult()
        {
            // Parse callback arguments using the CallbackUtility included with Web ADF.
            // CallbackEventArgument is a string member variable inherited from 
            // ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl.  Upon a callback, it 
            // contains the string argument passed to RaiseCallbackEvent().
            System.Collections.Specialized.NameValueCollection nameValueCollection =
                ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(
                this.CallbackEventArgument);

            int polylineLayerID;
            if (int.TryParse(nameValueCollection["layer"], out polylineLayerID))
            {
                // Set the property on the custom Editor that indicates the target layer for Polygon to Polyline
                CustomEditor customEditor = (CustomEditor)ParentEditor;
                customEditor.PolylineLayerID = polylineLayerID;
            }
            return null;
        }

        #endregion

        #region Instance Properties

        // The layer ID for the selected layer in the drop down list
        public int TargetLayerID
        {
            get
            {
                return int.Parse(m_layersDropDownList.SelectedValue);
            }
        }

        #endregion

        #region Instance Methods

        // Add editable polyline layers to drop down list
        private void AddPolylineLayers()
        {
            if (Page.Application == null)
                return;

            // Retrieve polyline layer names and ids from session
            System.Collections.Specialized.NameValueCollection polygonLayersNameValueCollection = 
                Page.Session["polygonLayers"] as System.Collections.Specialized.NameValueCollection;
            if (polygonLayersNameValueCollection == null)
            {
                polygonLayersNameValueCollection = new System.Collections.Specialized.NameValueCollection();
                // Iterate through the EditorTask's string list of editable layer IDs
                foreach (string stringLayerID in Task.EditableLayers)
                {
                    int layerID = int.Parse(stringLayerID);

                    // Iterate through the MapLayerInfos in the editor's underlying map resource
                    foreach (ESRI.ArcGIS.ADF.ArcGISServer.MapLayerInfo mapLayerInfo in 
                        ParentEditor.MapFunctionality.MapResource.MapServerInfo.MapLayerInfos)
                    {
                        // Check whether the current MapLayerInfo corresponds to that referred by the current layer ID
                        if ((mapLayerInfo.IsFeatureLayer) && (mapLayerInfo.LayerID == layerID))
                        {
                            // If the MapLayerInfo contains a polyline geometry definition add it to the collection of
                            // polyline layers
                            foreach (ESRI.ArcGIS.ADF.ArcGISServer.Field agsField in mapLayerInfo.Fields.FieldArray)
                            {
                                if (agsField.GeometryDef != null && agsField.GeometryDef.GeometryType == 
                                    ESRI.ArcGIS.ADF.ArcGISServer.esriGeometryType.esriGeometryPolyline)
                                {
                                    // If editable layer is of type polyline, add to the collection
                                    polygonLayersNameValueCollection.Add(stringLayerID, mapLayerInfo.Name);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
                Page.Session["polygonLayers"] = polygonLayersNameValueCollection;
            }

            // Add list items to drop down list
            foreach (string layerID in polygonLayersNameValueCollection.Keys)
                m_layersDropDownList.Items.Add(new System.Web.UI.WebControls.ListItem(
                    polygonLayersNameValueCollection[layerID], layerID));

            // Set custom Editor PolylineLayerID property
            CustomEditor customEditor = (CustomEditor)ParentEditor;
            if (customEditor.PolylineLayerID == -1 && polygonLayersNameValueCollection.Count > 0)
                customEditor.PolylineLayerID = int.Parse(polygonLayersNameValueCollection.Keys[0]);
        }

        #endregion
    }
}