引用AJaxPro.2.dll
index.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="index.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="888888" 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);
&nbs
p; //第1、2參數為服務器方法所需要的參數,後面一個是如果服務器返回數據
//客戶端要處理這些數據的JS函數名,他有個參數就是從服務器傳來的數據
MyDemo._Default.AddTwo(a1,b1,getAdd);
}
function getAdd(rel)
{
//要加上.value
alert(rel.value);
}
</script>
</body>
</Html>
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;
using AJaxPro;
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));
}
}
}