1. 程式人生 > >.net的web開發前後端傳值系列(一)

.net的web開發前後端傳值系列(一)

1.在webfrom頁面的AJAX傳值
頁面:

function SaveItemCraft(id) {
            var NewValue = $("#TextItemCraft_" + id).val();
            $.ajax({
                url: ("FinancialCenter.aspx?timestamp={0}").format(new Date().getTime()),
                type: 'POST', dataType: 'json', timeout: 10000,
                data: { Action: "UpdateItemCraft"
, Callback: "true", ItemId: id, UpdateValue: NewValue }, success: function (resultData) { if (resultData.STATUS == "SUCCESS") { $("#ItemCraft_" + id).text(NewValue); $("#imgItemCraft_" + id).show(); $("#TextItemCraft_"
+ id).hide(); $("#aItemCraft_" + id).hide(); $("#aItemCraft2_" + id).hide(); $("#ItemCraft_" + id).show(); } else { alert("系統忙請稍後再試!"); } } }); }

後臺:

using Jayrock.Json;


protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(base.Request.Form["Callback"]) && (base.Request.Form["Callback"] == "true"))
            {
                this.Controls.Clear();
                this.DoCallback();
            }
            if (!this.Page.IsPostBack)
            {
                。。。。
            }
        }

private void DoCallback()
        {
            string action = base.Request.Form["Action"];
            base.Response.Clear();
            base.Response.ContentType = "application/json";
            string s = string.Empty;
            if (action != null)
            {
                switch (action)
                {
                    case "UpdateItemCraft":
                        s = this.UpdateItemCraft();
                        break;
                }
            }
            base.Response.Write(s);
            base.Response.End();
        }


private string UpdateItemCraft()
        {
            JsonObject obj2 = new JsonObject();
            long itemId = Globals.SafeLong(base.Request.Form["itemId"], (long)0L);
            string newValue = Globals.SafeString(base.Request.Params["UpdateValue"], "");
            if (orderBll.UpdateCostPrice(itemId, decimal.Parse(newValue)))
            {
                obj2.Put("STATUS", "SUCCESS");
            }
            else
            {
                obj2.Put("STATUS", "FAILED");
            }
            return obj2.ToString();
        }