Asp.Net Web Form&&Ajax实现无刷新回调的两种方法

第一种  原始法

采用了Js Ajax原生的方法来实现

html代码:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
//创建Ajax对象
function CreateXmlHttp()
{
     try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
     catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
     catch (e) { try { xmlHttp = new XMLHttpRequest(); }
     catch (e) { xmlHttp = false; }}}
}

function ShowAjax(PID)
{ 
      var url="Process.ashx?PID="+ PID +"&rd="+Math.random();
      CreateXmlHttp();
      AjaxProcess(url,"spFile");
}

function AjaxProcess(url,id)
{
     xmlHttp.open("GET", url, true);
     xmlHttp.onreadystatechange = AjaxSumbitemployment;
     xmlHttp.send(null);
     function AjaxSumbitemployment()
     {
         if (xmlHttp.readyState == 4){
              if(xmlHttp.status==200)
              {
                   if(xmlHttp.responseText=="success")
                   {
                       alert("成功!");
                   }
                   else
                   {
                       alert("失败!");                          
                   }
              }
         }
     }   
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <input id="show1" type="button" value="显示1"  onclick='ShowAjax("1");' />        
    <input id="show2" type="button" value="显示2"  onclick='ShowAjax("2");' />
</div>
</form>
</body>
</html>

后端使用一般处理文件.ashx:

using System;
using System.Web;
public class Process: IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string PID = context.Request.QueryString["PID"];
        if (!String.IsNullOrEmpty(PID))
        {
            if (int.Parse(PID)==1)
            {
                context.Response.Write("success");
            }
            else
            {
                context.Response.Write("fail");
            }
        }
    } 

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

第二种 实现ICallBackEventHandler接口方法

html代码:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript">
    function CallServer(product)//请求服务器
    {
        <%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
    }    

    function ReceiveServerData(rValue) //接收服务器的返回值rValue
    {
        alert(rValue);
    }
    </script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <button onclick="CallServer('测试')" runat="server" id="btnTest">CallServer</button>
</div>
</form>
</body>
</html>

aspx.cs:

需要要实现接口ICallbackEventHandler

    private string CallBackValue = string.Empty;

    /// <summary>
    /// 处理并返回值到前台的ReceiveServerData
    /// </summary>
    /// <returns></returns>
    string ICallbackEventHandler.GetCallbackResult()
    {
        return CallBackValue + ",ok";
    }

    /// <summary>
    /// 获取并设置回调值
    /// </summary>
    /// <param name="eventArgument"></param>
    void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
    {
        this.CallBackValue = eventArgument;
    }

或者

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ClientScriptManager csManager = Page.ClientScript;
            string callbackMethod = csManager.GetCallbackEventReference(this, "document.getElementById('btnTest').value", "ReceiveServerData", "this.value");
            btnTest.Attributes.Add("OnClick", callbackMethod);
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值