webconfig配置
</system.web>
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, AJax" />
</httpHandlers>
</system.web>
定義一個類,執行服務器端操作,需要在客戶端調用的方法頭前加如下代碼
[Ajax.AjaxMethod(AJax.HttpSessionStateRequirement.Read)]
例如:
public class abc
{
[Ajax.AjaxMethod(AJax.HttpSessionStateRequirement.Read)]
public string getResult(string hl,string jine)
{
try
{
Conn con=new Conn();
decimal a=con.GethuilvByName(hl);
decimal b=Decimal.Parse(jine);
return ((decimal)(a*b)).ToString();
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
return null;
}
}
}
pageload裡注冊此類
Ajax.Utility.RegisterTypeForAJax(typeof(abc));
前端JS調用的時候類名.方法名
例
function getinformation()
{
var tmp = document.all("txtZhrmb");
var hl=document.all("DropDownList1").value;
var jine=document.all("txtJine").value;
tmp.value =abc.getResult(hl,jine).value;
}