ajax利用session传值(案例)

后台:

 public static string TrendChart()
        {
            HttpContext context = HttpContext.Current;
            ReturnStr strReturn = new ReturnStr();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            strReturn.msg = serializer.Serialize(list);
            strReturn.other = ExcelName;
            strReturn.oldname = priceType;
            strReturn.count = ProductID.Count;
            strReturn.success = true;
            if (context.Session["Object"] != null)
            {
                context.Session.Remove("Object");
            }
            context.Session["Object"] = strReturn;
            return serializer.Serialize(strReturn);
        }

前台:

 $("#btnTrendChart").bind("click", function () {
            var jsonData = {
                path: "/CollectAnalysis/LivelihoodAnalysis/ViewProductDetails.aspx/TrendChart",
                param: {}
            }
            var resultData = _Fun.jQueryAjax(jsonData);
            if (resultData.info) {
                WindowsOutOpen("ProductTrendChart.aspx");
            }                 
        });

还可以

 $("#btnTrendChart").bind("click", function () {
            var jsonData = {
                path: "/CollectAnalysis/LivelihoodAnalysis/ViewProductDetails.aspx/TrendChart",
                param: {}
            }
            var _myJson = { info: true };
            showProgress(0, "正在整理数据中,请稍候...");
            $.ajax({
                type: "POST",
                async: true,
                url: jsonData.path,
                data: JSON.stringify(jsonData.param),
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    _myJson.info = JSON.parse(data.d);
                    if (_myJson.info.success) {
                        WindowsOutOpen("ProductTrendChart.aspx");
                    }
                    hideProgress();
                },
                error: function () {
                    hideProgress();
                    error('服务器未响应,请联系管理员!');
                }
            });            
        });

这种可以添加进度条:showProgress(0, "正在整理数据中,请稍候...");

ProductTrendChart.aspx后台代码:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Session["Object"] == null)
                    {
                        //如果没有要导出的Session对象
                        return;
                    }
                    jsonObject = Session["Object"] as ReturnStr;
                    string jsonlist = jsonObject.msg;
                    JavaScriptSerializer Serializer = new JavaScriptSerializer();
                    list = Serializer.Deserialize<List<Models.livelihooddatachecked>>(jsonlist);
                }
                catch { }
            }
        }

 

posted @ 2014-06-08 13:20  改变你我  阅读(5916)  评论(0编辑  收藏  举报