Asp.Net2.0页面通过ICallbackEventHandler接口实现Ajax

1.页面继承ICallbackEventHandler接口。

2.编写服务器端代码:

#region ICallbackEventHandler

        string result = string.Empty;

        string ICallbackEventHandler.GetCallbackResult()
        {
            return result;
        }

        void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
        {
            string aid = "";//Ads id
            Regex r;
            Match m;

            if (eventArgument.Contains("method=getDetail"))
            {
                r = new Regex("(?<=(aid=))\\w*");
                m = r.Match(eventArgument);
                if (m.Success)
                    aid = m.Value;
                if (aid != string.Empty)
                {
                    result = GetAdDetail(aid);
                }
            }
        }

        /// <summary>
        /// 获得详情
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        private string GetAdDetail(string aid)
        {
            string r = string.Empty;
            IAdColumn ad=BkServices.PromotionService.GetAdColumn(aid);
            if (ad != null)
            {
                if (ad.AdColumnType == AdvertisingType.Infomation_Wanted)
                {
                    return string.Format("{0}&{1}", ad.Name, ad.Content);
                }
                else if (ad.AdColumnType == AdvertisingType.Infomation_Offering)
                {
 
                }
            }
            return r;
        }
        #endregion

3.编写客户端代码:

<script type="text/javascript">


var docEle = function()
{
    return document.getElementById(arguments[0]) || false;
}

function openNewDiv(_id,detail)
{
    //获得参数
    var items=detail.toString().split("&");
    var m = "mask";
    if (docEle(_id)) document.body.removeChild(docEle(_id));
    if (docEle(m)) document.body.removeChild(docEle(m));
   
    //mask遮罩层

    var newMask = document.createElement("div");
    newMask.id = m;
    newMask.style.position = "absolute";
    newMask.style.zIndex = "1";
    _scrollWidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
    _scrollHeight = Math.max(window.screen.availHeight,document.documentElement.scrollHeight);
    newMask.style.width = _scrollWidth + "px";
    newMask.style.height = _scrollHeight + "px";
  
    newMask.style.top = "0px";
    newMask.style.left = "0px";
    newMask.style.background = "#999999";
    newMask.style.filter = "alpha(opacity=40)";
    newMask.style.opacity = "0.40";
   
    document.body.appendChild(newMask);
   
    //新弹出层

    var newDiv = document.createElement("div");
    newDiv.id = _id;
    newDiv.style.position = "absolute";
    newDiv.style.zIndex = "9999";
    newDiv.className="";
    newDivWidth = 562;
    newDivHeight = 260;
    newDiv.style.top = (document.body.scrollTop + document.body.clientHeight/2 - newDivHeight/2) + "px";
    newDiv.style.left = (document.body.scrollLeft + document.body.clientWidth/2 - newDivWidth/2) + "px";
    var detail="<div class='pay_layer'><h1>详细信息</h1><h6 class='mt10 yellow_Bg'><font class='red'>信息标题:</font>"+items[0]+"</h6><h6 class='mt10 yellow_Bg'><font class='red'>详细介绍:</font><h5 class='f14 fnormal'>"+items[1]+"</h5></h6><h2><input type='button' value='确 认' style='margin:0 0 0 0; 60px;' onclick='newDiv.closeMask()'/></h2></div>";
    newDiv.innerHTML = detail;
    document.body.appendChild(newDiv);
   
    //弹出层滚动居中

    function newDivCenter()
    {
        newDiv.style.top = (document.body.scrollTop + document.body.clientHeight/2 - newDivHeight/2) + "px";
        newDiv.style.left = (document.body.scrollLeft + document.body.clientWidth/2 - newDivWidth/2) + "px";
    }
    if(document.all)
    {
        window.attachEvent("onscroll",newDivCenter);
    }
    else
    {
        window.addEventListener('scroll',newDivCenter,false);
    }
   
    //关闭新图层和mask遮罩层
    newDiv.closeMask = function()
    {
        if(document.all)
        {
            window.detachEvent("onscroll",newDivCenter);
        }
        else
        {
            window.removeEventListener('scroll',newDivCenter,false);
        }
        document.body.removeChild(docEle(_id));
        document.body.removeChild(docEle(m));
        return false;
    }
    newDiv.appendChild(newA);
   
}

function closeWin()
{
       alert();
        if(document.all)
        {
            window.detachEvent("onscroll",newDivCenter);
        }
        else
        {
            window.removeEventListener('scroll',newDivCenter,false);
        }
        document.body.removeChild(docEle('_id'));
        document.body.removeChild(docEle('m'));
        return false;
}

</script>

    <script type="text/javascript" language="javascript">  
        //ajax显示详情
  
  function getDetail(aid)
  {

      {

      var param="method=getDetail&aid="+aid;
      <%=Page.ClientScript.GetCallbackEventReference(this,"param", "setDetail", "")%>;

      }
   
  }
  function setDetail(result,context)
  {
     
      openNewDiv('newDiv',result);
      return false;
     
  }

    </script>

原文地址:https://www.cnblogs.com/olartan/p/1371876.html