可以使用一個自訂計算函數,在自訂計算函數中使用ajax調用後台服務,做到伺服器端計算。
定義計算函數
定義一個自訂計算函數,並調用後台服務
// 調用服務端計算
function callservices(price, qty) {
var rv;
YZSoft.Ajax.request({
url: YZSoft.$url('Demo/Samples/Services.ashx'),
async:false,
params:{
method:'Calc',
price:price,
qty:qty
},
success:function(action){
rv = action.result;
}
});
return rv;
}
實現計算服務
後台定義一個Services.ashx文件,程式碼如下:
<%@ WebHandler Language="C#" Class="YZDemoServices" %>
using System;
using System.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
public class YZDemoServices : YZServiceHandler
{
public decimal Calc(HttpContext context)
{
YZRequest request = new YZRequest(context);
decimal price = request.GetDecimal("price", 0);
decimal qty = request.GetDecimal("qty", 0);
return price qty;
}
}
Express設置
javascript('callservices',單價,數量)