ADF Tutorials
ADFTutorials_CSharp\ScriptableControls\MapCoordinateDisplay\MapCoordinateDisplay.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.
// 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MapCoordinateDisplay
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:MapCoordinateDisplay runat=server></{0}:MapCoordinateDisplay>")]
    [AjaxControlToolkit.ClientScriptResource("MapCoordinateDisplay.MapCoordinateDisplay",
        "MapCoordinateDisplay.javascript.MapCoordinateDisplay.js")]
    public class MapCoordinateDisplay : ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl
    {
        private Label m_displayLabel;

        public string Text
        {
            get
            {
                return StateManager.GetProperty("text") as string;
            }
            set
            {
                StateManager.SetProperty("text", value);
            }
        }

        /// <summary>The ID of the Map control to associate with this control.</summary>
        public string Map
        {
            get
            {

                return StateManager.GetProperty("map") as string;
            }
            set
            {
                StateManager.SetProperty("map", value);
            }
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
            base.CreateChildControls();

            m_displayLabel = new Label();
            m_displayLabel.ID = "DisplayLabel";
            m_displayLabel.Text = Text;
            m_displayLabel.Font.Bold = true;
            Controls.Add(m_displayLabel);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (!base.IsAsync)
            {
                StringBuilder script = new StringBuilder();
                script.Append("Sys.Application.add_init(function() {");
                script.Append("$create(MapCoordinateDisplay.MapCoordinateDisplay,");
                script.AppendFormat("{{\"displayLabel\":$get('{0}')", m_displayLabel.ClientID);
                script.AppendFormat("}}, null, {{\"map\":\"{0}\"}}, $get('{1}'));", Map, ClientID);
                script.AppendLine("});");

                ScriptManager.RegisterStartupScript(this, typeof(MapCoordinateDisplay), this.ClientID + "_startup", script.ToString(), true);
            }
        }
    }
}