Working with the Web ADF resources in a custom task at run-time

Rex Hansen of the .NET SDK team wrote this very useful post on accessing the Web ADF components at run-time.

 

When creating a custom task, you may often find it necessary to access Web ADF components (controls, resources, etc.) that share the same Web page with the task.  How the task will be used will dictate the technique for accessing Web ADF components.  There are three situations to consider:

1) Task run-time

2) Visual Studio design-time

3) Manager run-time

“Task run-time” defines the technique for working with Web ADF components in a Web application at run-time.  “Visual Studio design-time” uses custom verbs on the custom task control to access to Web ADF components during task configuration in Visual Studio.  “Manager run-time” defines a situation where a custom task must utilize Web ADF components when configuring the task in the Manager Web application.  

This discussion and walkthrough will focus on working with Web ADF resources in a custom task at run-time (situation 1).  The custom task will do the following:

  • Extend FloatingPanelTask
  • Expose a public property to buddy the task with a MapResourceManager
  • Use a DropDownList control to list the map resources in the MapResourceManager control
  • Populate the DropDownList upon initial load of the task

1) Create a custom task control that extends FloatingPanelTask.  As a composite control, the custom task will contain multiple controls.  Define two private member variables to store references to controls that will be used in the custom task.  In this case, a Label will be used to display the MapResourceManager id and a DropDownList will display a list of map resources.

namespace TestTask_CSharp

{

    public class TestTask_Demo : FloatingPanelTask

    {

        private Label label = null;

        private DropDownList dropDownListResources = null;

2) Create a property on the custom task control to store a reference to the unique id of a MapResourceManager control.  The property will be maintained in state for the duration of a user session.  The property can be set declaratively in the aspx page using the MapResourceManagerID attribute (source view). 

        [Browsable(true)]

        [DefaultValue("")]

        [PersistenceMode(PersistenceMode.Attribute)]

        public string MapResourceManagerID

        {

            get

            {

                object obj = StateManager.GetProperty("mapResourceManagerID");

                if (obj == null) return "";

                return obj as string;

            }

            set

            {

                StateManager.SetProperty("mapResourceManagerID", value);

            }

        }

3) As with other composite Web controls, override the CreateChildControls method to construct the custom task control at runt
ime.  The Label and DropDownList are created and added to the custom task.  Note the DropDownList is empty.  The Controls property is inherited from the System.Web.UI.WebControls.CompositeControl class and maintains a collection of controls to be rendered at runtime.   

        protected override void CreateChildControls()

        {

            Controls.Clear();

            base.CreateChildControls();

            label = new Label();

            label.Text = MapResourceManagerID;

            label.ID = "label_mrm";

            dropDownListResources = new DropDownList();

            dropDownListResources.ID = "dropdownlist_resources";

            Controls.Add(label);

            Controls.Add(dropDownListResources);

        }

4) At this time we only need to populate the DropDownList (dropDownListResources) with a list of map resources upon initial load of the custom task.  Since interrogating the MapResourceManager to get a list of resources during every callback is not necessary and can be expensive, we only want it to occur during a full page postback.  The PreRender step in the page lifecycle offers a good location to modify page or control content before the final rendering step, and it's only called during a full page postback.  Override the OnPreRender event of the custom task control and call the UpdateResourceDropDownList() method.  The method will contain the logic to update the dropDownListResources control.  This logic was placed in a separate method so it can be called from other locations in the custom task code, if necessary in the future.        

        protected override void OnPreRender(EventArgs e)

        {

            base.OnPreRender(e);

            UpdateResourceDropDownList();

        }

       

5) The UpdateResourceDropDownList() method uses the MapResourceManagerID property to find a MapResourceManager control on the page and get a list of map resources.  The GetMapResourcesFromMRM() method returns a collection of resources from the MapResourceManager.  If the MapResourceManager has not been initialized, it will be explicitly initialized. Including code to return a collection of resources from a MapResourceManager was placed in a separate method so it can be called from other locations in the custom task that do not involve the dropDownListResources control.  Once a list of resources is returned to the UpdateResourceDropDownList method, only those that are an ArcGIS Server map resource are added to the dropDownListResources control.           

        protected void UpdateResourceDropDownList()

        {

            if (!string.IsNullOrEmpty(MapResourceManagerID))

            {

                GISResourceCollection grcoll = GetMapResourcesFromMRM(MapResourceManagerID);

                foreach (IGISResource gr in grcoll)

                {

 &
nbsp;                  if (gr is ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceBase)

                    {

                        dropDownListResources.Items.Add(gr.Name);

                    }

                }

            }

        }

        protected GISResourceCollection GetMapResourcesFromMRM(string mrmID)

        {

            GISResourceCollection gisRC = new GISResourceCollection();

            MapResourceManager rm = (MapResourceManager)Page.FindControl(mrmID);

            if (rm == null)

                return null;

            if (!rm.Initialized)

                rm.Initialize();

            foreach (MapResourceItem mri in rm.ResourceItems)

            {

                gisRC.Add(mri.Resource);

            }

            return gisRC;

        }

6) Override a set of methods associated with task implementation to complete the custom task control.  Further details on these methods will be provided in future blog posts.

        public override string GetCallbackResult()

        {

            return base.GetCallbackResult();

        }

        public override void ExecuteTask()

        {}

        public override List<GISResourceItemDependency> GetGISResourceItemDependencies()

        {

            List<GISResourceItemDependency> list = new List<GISResourceItemDependency>();

            return list;

        }

    }

}

7) Build the custom task in Visual Studio and add a reference to it in a Web page.  Add the custom task and a MapResourceManager control to the page.  Add one or more ArcGIS Server resources to the MapResourceManager.  Run the Web app and you should see a drop down list containing the name of each ArcGIS Server map resource item in the MapResourceManager.    It should look similar to the following screenshot:

 

 

 

You can download the sample here.

 

This entry was posted in Services and tagged , , . Bookmark the permalink.

Leave a Reply

One Comment

  1. anoopjayaram says:

    Any idea about How to add items dynamically to resource manager?