代码改变世界

图层中获取某个图元的id值【原创】

2008-08-04 15:49  Jeffery Tao  阅读(329)  评论(0)    收藏  举报

a、使用PointSelectionTool1组件,设置属性:
ClientCommand:InfoCommandID --这个是自己定义的属性,放在Command.js
ClientInteraction:ClickInteraction、
Command:Infoid --这个也是自己定义的属性,放在了一个通用类中

代码如下:

    App_Code/CustomCommands2.cs

    /// <summary>
    /// Info command for InfoWebTool.
    /// </summary>
    [Serializable]
    public class Infoid : MapInfo.WebControls.MapBaseCommand
    {
        /// <summary>
        /// Key to be used to get the pixel tolerance parameter value from the URL.
        /// </summary>
        protected const string PixelToleranceKey = "PixelTolerance";
        protected const string InfoCommand = "Infoid";

        bool YesNo = false;
        public Infoid()
        {
            Name = InfoCommand;
        }
       
      
        /// <summary>
        /// Override the Execute method in MapBasicCommand class to not save state, because
        /// for info tool, which does not change map state, so there is no need to save map state.
        /// </summary>
        public override void Execute()
        {

            StateManager sm = StateManager.GetStateManagerFromSession();
            if (sm == null)
            {
                if (StateManager.IsManualState())
                {
                    throw new NullReferenceException("Cannot find instance of StateManager in the

ASP.NET session.");
                }
            }
            ParseContext();
            if (sm != null)
            {
                PrepareStateManagerParamsDictionary(sm);
                sm.RestoreState();
            }

            Process();
        }

        /// <summary>
        /// method to do the real server side process for info tool.
        /// </summary>
        public override void Process()
        {
            try
            {
                //get pixel tolerance from url of client side.
                string ss = HttpContext.Current.Request[PixelToleranceKey];
                int pixelTolerance = System.Convert.ToInt32(HttpContext.Current.Request

[PixelToleranceKey]);

                MapControlModel model = MapControlModel.GetModelFromSession();
                model.SetMapSize(MapAlias, MapWidth, MapHeight);

                //extract points from url of client side.
                System.Drawing.Point[] points = ExtractPoints(DataString);

                //do searching and get results back
                MultiResultSetFeatureCollection mrfc = RetrieveInfo(points, pixelTolerance);

                IEnumerator resultEnum = mrfc.GetEnumerator();

                //retrieve the selected feature from collection
                while (resultEnum.MoveNext())
                {
                    IResultSetFeatureCollection irfc = (IResultSetFeatureCollection)

resultEnum.Current;
                    IFeatureEnumerator ftrEnum = irfc.GetFeatureEnumerator();

                    while (ftrEnum.MoveNext())
                    {
                        Feature ftr = (Feature)ftrEnum.Current;
                        //create a html table to display feature info and stream back to client side.

                        CreateInfoTable(ftr);
                        irfc.Close();
                        mrfc.Clear();
                        break;
                    }
                    break;
                }
            }
            catch { }
        }

  private void CreateInfoTable(Feature ftr)
        {
            string Country = "";
            string zp = "";
            string SP = "";
            try
            {
                //create a table control and populat it with the column name/value(s) from the

feature returned and
                // and the name of the layer where the feature belong
                System.Web.UI.WebControls.Table infoTable = new System.Web.UI.WebControls.Table();
                //set table attribute/styles
                infoTable.CellPadding = 4;
                infoTable.Font.Name = "Arial";
                infoTable.Font.Size = new FontUnit(8);
                infoTable.BorderWidth = 1;
                //infoTable.BorderSte = BorderStyle.Outset;

                System.Drawing.Color backColor = Color.Bisque;

                //add the first row, the layer name/value where the selected feature belongs
                TableRow r = new TableRow();
                r.BackColor = backColor;

                TableCell c = new TableCell();
                c.Font.Bold = true;
                c.ForeColor = Color.Indigo;

                c.Text = "图层名称";
                r.Cells.Add(c);

                c = new TableCell();

                //the feature returned is from a resultset table whose name is got from appending _2
                //to the real table name, so below is to get the real table name.
                string alias = ftr.Table.Alias;
                c.Text = alias.Substring(0, alias.Length - 2);
                c.Font.Bold = true;
                r.Cells.Add(c);

                //infoTable.Rows.Add(r);
                //infoTable.Rows.Add(r);
                if (c.Text == "政府周围建筑")
                {
                    foreach (Column col in ftr.Columns)
                    {
                        String upAlias = col.Alias.ToUpper();
                        //don't display obj, MI_Key or MI_Style columns
                        if (upAlias != "OBJ" && upAlias != "MI_STYLE" && upAlias != "MI_KEY")
                        {

                            if (col.Alias == "政府周围建筑")
                            {
                                Country = ftr[col.Alias].ToString();
                            }


                        }
                    }
                  
                  HttpContext.Current.Response.Output.Write(Country+"@");
                
                }
                // HttpContext.Current.Response.Output.Write("<img 

src='SubModule/glsynk/ReadImage.aspx?ID=" + Ds.Tables[0].Rows[0]["id"].ToString()+ "' id='image2'

style='width: 200px; height: 209px' /><br>" + strHTML);
                // }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

-----Command.js

function InfoCommandID(name, interaction)
{
 if (arguments.length > 0)
        {
    this.Init(name, interaction);
 }
}

InfoCommandID.prototype = new MapCommand();
InfoCommandID.prototype.constructor = InfoCommandID;
InfoCommandID.superclass = InfoCommandID.prototype;
InfoCommandID.prototype.Execute = function()
{
 this.CreateUrl();
 this.AddParamToUrl("PixelTolerance", this.pixelTolerance);
 //create an XMLHttp obj to send request to server
 var xmlHttp = CreateXMLHttp();
 xmlHttp.open("GET", this.url, false);
 xmlHttp.send(null);
 //get response back
 this.result = xmlHttp.responseText; 
 var friend_array =this.result.split("@");
    var url="../LCXS.aspx?XQID="+friend_array[0];
    window.open(url, 'newwindow',

'width=1000px,height=700px,toolbar=yes,status=yes,menubar=yes,resizable=yes,scrollbars=yes')    
 
};