jquery getJSON 数据联动(采用序列化和反序列化获取数据)



   
   
   


   


   

       
       
       
   

   


 

SelectAjax.ashx文件:

///


    /// SelectAjax 的摘要说明
    ///

    public class SelectAjax : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string action = context.Request.QueryString["action"];


            if (("area").Equals(action))
            {
                string id = context.Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id))
                    context.Response.Write(GetArea(id));
                else context.Response.Write("");
            }
        }


        private string GetArea(string id)
        {
            List areaList = new List();

            if (id == "0")
            {
                areaList.Add(new Area
                {
                    AreaID = "01",
                    AreaName = "罗湖区",
                    PID = "0"
                });
                areaList.Add(new Area
                {
                    AreaID = "02",
                    AreaName = "福田区",
                    PID = "0"
                });
            }

            if (id == "01")
            {
                areaList.Add(new Area
                {
                    AreaID = "0101",
                    AreaName = "莲塘",
                    PID = "01"
                });
                areaList.Add(new Area
                {
                    AreaID = "0102",
                    AreaName = "黄贝岭",
                    PID = "01"
                });
            }
            if (id == "02")
            {
                areaList.Add(new Area
                {
                    AreaID = "0201",
                    AreaName = "八卦岭",
                    PID = "02"
                });
                areaList.Add(new Area
                {
                    AreaID = "0202",
                    AreaName = "华强",
                    PID = "02"
                });
            }

            return Serialize(areaList);
        }

        ///


        /// Json序列化
        ///

        /// 泛型
        /// 泛型
        /// 序列化
        private string Serialize(object obj)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(obj);
        }
        ///
        /// Json反序列化
        ///

        /// 泛型
        /// 泛型
        /// 反序列化
        private T Deserialize(string strJson)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Deserialize(strJson);
        }

        public class Area
        {
            public string AreaID { get; set; }
            public string AreaName { get; set; }
            public string PID { get; set; }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

 

你可能感兴趣的:(JQuery,ASP.NET技术)