ajax

几种方法:
第一种:
ajaxpro:http://www.xker.com/edu/dev/107/066210063670764.html
1、添加VS2005引用AJAX2.0程序集(AjaxPro.2.dll)
2、 在web.config文件中的<system.web>加入以下设置

<httpHandlers>
 <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>
3. 创建一个页面Default.aspx

  4. 在Default.aspx.cs文件的Page_Load中注册AJAX可调用的类的名称

protected void Page_Load(object sender, EventArgs e)
{
 AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
}

  5. 创建AJAX可调用的方法:

[AjaxPro.AjaxMethod]
public string SetTb(string name)
{
 return name;
}

  注意:[AjaxPro.AjaxMethod]是定义AjaxPro可调用的方法,是必须要注明的

  6. 前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
// <!CDATA[

function comit_onclick()
{
 var name=document.getElementById("tb1").value;
 _Default.SetTb(name,callback);
}
function callback(res)
{
 document.getElementById("tb").value=res.value;
}

// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="tb1" runat="server"></asp:TextBox><br />
<input id="comit" type="button" value="Ok" onclick="return comit_onclick()" />
<br /><asp:TextBox ID="tb" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>


  注意:这里值得注意的地方是 _Default.SetTb(name,callback);这句话是为了调用_Default.aspx.cs后台代码中SetTb这个方法的,如果这个方法没有要传递的参数则指明返回的处理方法是哪一个就OK了,写成_Default.SetTb(callback);
第二种:不需要设置。
1、<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language='javascript'>
 function getResponseData(params)
     {
            var objHttp = new ActiveXObject("Microsoft.XMLHTTP");//更加完善的是判断浏览器的类型
            var getStr;
            objHttp.Open("Get","Default.aspx?params="+params,false);
            objHttp.Send();
            getStr = objHttp.responseText;

            return getStr;
            }
    


function Button1_onclick() {
var tt= getResponseData("getData");
document.getElementById("TextBox1").innerText=tt;
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" value="button" language="javascript" onclick="return Button1_onclick()" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
    </form>
</body>
</html>
2、using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["params"] == "getData")//判断是不是xmlhttp的请求,自创,没有找到办法。
            getData();

    }
   
    public void  getData()
    {
   // Public Function GetEmployee(ByVal strEmployeeId As String) As String
        Response.Clear();
        Response.Write("0");
        Response.End();         
       
    }
}
第三种:回调
1、<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type="text/javascript">

    function GetMessageFromServer()

    {

        UseCallBack1("","");

    }

   

    function JSCallback(TextBox1, context)

    {

        // 当回调结束后将执行这里的方法,把从服务器端获取的值进行客户端操作

        document.forms[0].TextBox1.value = TextBox1;

    }

</script> 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="button" id="Button1" runat="server" value="Get Message" onclick="GetMessageFromServer()"/>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
    </form>
</body>
</html>
2、
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default3 : System.Web.UI.Page,ICallbackEventHandler
{
    string callbackResult;   
    protected void Page_Load(object sender, EventArgs e)
    {
        #region
        //下面的C#代码在运行时,将在页面生成如下的JavaScript代码:
        //function UseCallBack(arg, context)
        //{
        //WebForm_DoCallback('__Page',arg,JSCallback,context,null,false);
        //}
        //实际上通过XmlHttp,最终从服务器端返回结果回调客户端方法。所以在服务器端执行完成之后框架将通知JavaScript方法告知服务器已经执行完毕,例子中将调用function JSCallback(TextBox1, context).


        string cbref = Page.ClientScript.GetCallbackEventReference(this, "arg", "JSCallback", "context");

        // 生成一个JavaScript脚本来触发回调

        string cbScr = string.Format("function UseCallBack1(arg, context) {{ {0}; }} ", cbref);

        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallBack1", cbScr, true);
        #endregion


    }
    public void RaiseCallbackEvent(string eventArgument)
    {

        // 服务器端操作

        callbackResult = "DotNET Rocks!";

    }

    public string GetCallbackResult()
    {

        return callbackResult;

    }


}
第四种:http://www.kingmx.com/article/8311
总结:1、第一种需要添加应用ajaxpro.dll,配置webconfig,组册.期中ajaxpro.dll与ajax.dll 个人认为用法
                  上没 有 区  别,前者是更高版本,没有具体尝试ajax.dll。
            2、第二种需要判断是不是xmlhttp的请求。比较简单,自创。不知道如何判断请求,通过参数。
            3、第三种 需要用到ICallbackEventHandler,个人认为就是一个接口,重要的方法
                    RaiseCallbackEvent和GetCallbackResult,这个理解起来没有第一种容易理解。     
            4、 最后一种和第三中差不多,参数设置,组册sript的方法可能不一样。
            5、本来很容易的东西,让他们用第一种、第三种的办法掩盖了显示麻烦了,用起来似乎简单点,
                   但是看不到ajax的本质东西。
Gridview
如何更新GridView,用MagicAjax.dll   
  AjaxPro.NET框架生成高效率的Tree(Asp.net
该文章转载自网络大本营:http://www.xrss.cn/Info/4769.Html
http://www.xrss.cn/Info/4769.Html

原文地址:https://www.cnblogs.com/lgzh3/p/804886.html