1.添加 ajaxpro.2.dll 引用 点击下载此文件
web.config
程序代码
<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
</httpHandlers>
</system.web>
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
</httpHandlers>
</system.web>
default.aspx.cs
程序代码
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;
namespace MyDemo
{
public partial class _Default : System.Web.UI.Page
{
[AjaxPro.AjaxMethod]
public DateTime GetServerTime()
{
return DateTime.Now;
}
[AjaxPro.AjaxMethod]
public int AddTwo(int firstInt, int secondInt)
{
return firstInt + secondInt;
}
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
}
}
}
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;
namespace MyDemo
{
public partial class _Default : System.Web.UI.Page
{
[AjaxPro.AjaxMethod]
public DateTime GetServerTime()
{
return DateTime.Now;
}
[AjaxPro.AjaxMethod]
public int AddTwo(int firstInt, int secondInt)
{
return firstInt + secondInt;
}
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
}
}
}
default.aspx
程序代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="MyDemo._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>
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" action="test1.aspx">
<div>
<input id="Button1" type="button" value="获得服务器时间" onclick="getServerTime();" />
<input id="Text1" type="text" />
<input id="Text2" type="text" />
<input id="Button2" type="button" value="得到两个文本框的和" onclick="add(document.getElementById('Text1').value,document.getElementById('Text2').value)" />
</div>
<input type="text" value="111111" name="testtext"/>
<input type="submit" name="testsubmit" value="测试"/>
</form>
<script type="text/javascript">
function getServerTime()
{
//MyDemo._Default.GetServerTime()得到从服务器传来的数据是object,要写.value
alert(MyDemo._Default.GetServerTime().value);
}
function add(a,b)
{
//把文本框的值转换成int
var a1 = parseInt(a);
var b1 = parseInt(b);
//第1、2参数为服务器方法所需要的参数,后面一个是如果服务器返回数据
//客户端要处理这些数据的js函数名,他有个参数就是从服务器传来的数据
MyDemo._Default.AddTwo(a1,b1,getAdd);
}
function getAdd(rel)
{
//要加上.value
alert(rel.value);
}
</script>
</body>
</html>
=================================================
========下面一个更简单例子========================
==============================================
1.添加 ajaxpro.2.dll 引用 点击下载此文件
2.在web.config中添加 httpHandlers
程序代码
<system.web>
<httpHandlers>
<add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>
</system.web>
<httpHandlers>
<add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>
</system.web>
add 这里还可以是
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
3.服务器端代码,我是新建 的一个空的 Ajax.aspx 文档才成功的。
Ajax.aspx.cs
程序代码
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 Ajax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(Ajax));
}
[AjaxPro.AjaxMethod]
public string GetServerTimeString()
{
System.Threading.Thread.Sleep(2000);
return DateTime.Now.ToString();
}
}
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 Ajax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(Ajax));
}
[AjaxPro.AjaxMethod]
public string GetServerTimeString()
{
System.Threading.Thread.Sleep(2000);
return DateTime.Now.ToString();
}
}
4.客户端代码
Ajax.aspx
程序代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ajax.aspx.cs" Inherits="Ajax" %>
<!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>ajaxpro.2.dll 简单应用</title>
<script language="javascript" type="text/javascript">
function ajax_callback(response)
{
document.getElementById("div1").innerHTML = response.value;
}
function getServerTime()
{
document.getElementById("div1").innerHTML = "请稍后,正在获取服务器时间..";
Ajax.GetServerTimeString(ajax_callback);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="div1">点击按钮获取服务器端时间</div>
<input type="button" value=" click " onclick="javascript:getServerTime();" />
</div>
</form>
</body>
</html>
<!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>ajaxpro.2.dll 简单应用</title>
<script language="javascript" type="text/javascript">
function ajax_callback(response)
{
document.getElementById("div1").innerHTML = response.value;
}
function getServerTime()
{
document.getElementById("div1").innerHTML = "请稍后,正在获取服务器时间..";
Ajax.GetServerTimeString(ajax_callback);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="div1">点击按钮获取服务器端时间</div>
<input type="button" value=" click " onclick="javascript:getServerTime();" />
</div>
</form>
</body>
</html>