AjaxPro.Utility.RegisterTypeForAjax(typeof(Resin));
使用上面的方法註冊當前頁面,運行後在頁面中加入下面兩句
<script type="text/javascript" src="/AJAXDemo/ajaxpro/core.ashx"></script>
<script type="text/javascript" src="/AJAXDemo/ajaxpro/AJAXDemo.Resin,AJAXDemo.ashx"></script>
<script language="javascript">
var response = Sample.ServerSideAdd(100,99);
alert(response.value);
</script>
增加用戶端可調用方法
public int ServerSideAdd(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}
用戶端調用
<form id="Form1" method="post" runat="server">
<script language="javascript">
var response = AJAXDemo.Resin.ServerSideAdd(100,99);
alert(response.value);
</script>
</form>
同時可以改下如下,這樣會返回一個response對象,可以對程式異常進行處理
<form id="Form1" method="post" runat="server">
<script language="javascript">
//var response = AJAXDemo.Resin.ServerSideAdd(100,99);
//alert(response.value);
AJAXDemo.Resin.ServerSideAdd(100,99,ServerSideAdd_CallBack);
function ServerSideAdd_CallBack(response){
if (response.error != null){
alert(response.error);
return;
}
alert(response.value);
}
</script>
</form>
處理dataset的例子
服務端
public DataSet GetDataSet()
{
// SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings["connString"], "GetHostitalListByPymAndGroupid",querycode,groupid);
DataSet myds = new DataSet();
myds=SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["connString"],CommandType.Text,"Select top 10 ypm,gg,cd,zxdw from zy_sys2_ypzdk");
return myds;
}
用戶端
<script language="javascript">
AJAXDemo.Resin.GetDataSet(GetDataSet_CallBack);
function GetDataSet_CallBack(response){
var ds = response.value;
if(ds != null && typeof(ds) == "object" && ds.Tables != null){
var s = new Array();
s[s.length] = "<table border=1>";
for(var i=0; i<ds.Tables[0].Rows.length; i++){
s[s.length] = "<tr>";
s[s.length] = "<td>" + ds.Tables[0].Rows[i].ypm + "</td>";
s[s.length] = "<td>" + ds.Tables[0].Rows[i].gg + "</td>";
s[s.length] = "</tr>";
}
s[s.length] = "</table>";
alert(s.join(""));
}
else
{
alert("Error. [3001] " + response.error);
}
}
</script>