A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘建素 中级黑马   /  2012-7-23 16:37  /  1532 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

这几天工作用到了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);        }

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

1 个回复

倒序浏览
讲的太棒了,值得学习!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马