这几天工作用到了ajaxpro,因为以前没用过所以这里小小的总结一下。
AjaxPro通过js访问服务端.net的免费库,它能把js请求发送到.NET方法,服务端传回给Javascript
首先要在webcofig文件下<httpHandlers>节点中间 添加
<add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
然后将AjaxPro.2.dll 引入站点的bin下
以我做的一个例子来说一下:
protected override void OnInit(EventArgs e)
{
if (this.SkinName == null)
{
this.SkinName = "Skin-CheckOut.html";
}
//向页面注册AJAX方法
AjaxPro.Utility.RegisterTypeForAjax(typeof(Bizproud.Web.Components.CheckOut));
base.OnInit(e);
}
我这里将一个checkout注册然后这个类里写的所有ajaxpro的方法可以在前台使用js调到
/// <summary>
/// 订单页面
/// </summary>
[ParseChildren(true)]
public class CheckOut : HtmlTemplatedWebControl
{}
然后我再checkout类写了一个遍历加入购物车的商品方法为ajaxpro 返回为json对象
/// <summary>
/// 得到购物车商品列表
/// </summary>
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
public string GetCartList()
{
string jsonString = "";
JArray arrProduct = new JArray();
foreach (Biz.Model.ShoppingCart.Product p in Order.Products.GetList())
{
jsonString = "{ID:'" + p.ID + "',"
+ "ClassifyID:'" + p.ClassifyID + "',"
+ "BrandID:'" + p.BrandID + "',"
+ "Code:'" + p.Code + "',"
+ "Name:'" + p.Name + "',"
+ "PromotionID:'" + p.PromotionID + "',"
+ "Price:'" + Bizproud.SystemServices.Globals.GetPriceCount(p.Price) + "',"
+ "OrderPrice:'" + Bizproud.SystemServices.Globals.GetPriceCount(p.OrderPrice) + "',"
+ "ScorePrice:'" + p.ScorePrice + "',"
+ "Integral:'" + p.Integral + "',"
+ "Rebate:'" + Bizproud.SystemServices.Globals.GetPriceCount(p.Rebate) + "',"
+ "Count:'" + p.Count + "',"
+ "IncludMailing:'" + p.IncludMailing + "',"
+ "ActivityDescription:'" + p.ActivityDescription + "',"
+ "SaleForm:'" + Convert.ToInt32(p.SaleForm).ToString() + "',"
+ "PriceTotal:'" + Bizproud.SystemServices.Globals.GetPriceCount(p.PriceTotal)
+ "'}";
JObject json = JsonConvert.DeserializeObject(jsonString) as JObject;
arrProduct.Add(json);
}
return arrProduct.ToString();
}
其中的json处理需要引入Newtonsoft.Json.dll
而我在前台就可以直接调用GetCarList()方法来得到购物车中商品
//显示购物车列表 function LoadCartList() { var cartList = eval(Bizproud.Web.Components.CheckOut.GetCartList().value); var html = ""; $.each(cartList, function (i, n) { html += '<tr>'; html += '<td bgcolor="#ffffff">'; html += '<a href="ProductShow.html?id=' + n.ID + '" target="_blank">' + n.Name + '</a></td>'; html += '<td bgcolor="#ffffff">¥' + n.OrderPrice + '</td>'; html += '<td bgcolor="#ffffff">' + n.Count + '</td>'; html += '<td bgcolor="#ffffff">¥' + n.PriceTotal + '</td>'; html += '</tr>'; }); $("#ProductList").html(html); }
|