Ajax實現前台傳值到后台


實現功能:通過Json格式將值由前台傳入后台

1.前台JS代碼

<script type="text/javascript" src="../Common/Javascript/jquery-1.4.2.js"></script>
<script type="text/javascript" language="javascript">
//實現編輯功能
function editCarsInfo(Eid) {
var Up_CompanyName = $("#txtCompanyName"), Up_CarLicence = $("#txtCarLicence"), Up_LinkMan = $("#txtLinkMan"), Up_Phone = $("#txtPhone"), Up_Transport_type = $("#drpTransport_type"), Up_isDelCar = $("#isDelCar"); var parm = "addCarsInfo"; jQuery.ajax({ type: "get", url: "AddTransCarManager.aspx?method=" + parm,//頁面地址和傳遞的URL參數
contentType: "application/json; charset=UTF-8",
dataType: "jsonp", data: { id: Eid,
CompanyName: escape(Up_CompanyName.val()),
//要向后台傳的值-escape對傳值進行編碼
CarLicence: escape(Up_CarLicence.val()),
LinkMan: escape(Up_LinkMan.val()),
Phone: escape(Up_Phone.val()),
Transport_type: escape(Up_Transport_type.val()),
isDelCar: escape(Up_isDelCar.val())
}, success: msg() }); function msg() { alert('修改汽車運輸信息成功'); $("#txtCompanyName").val(''); $("#txtCarLicence").val(''); $("#txtLinkMan").val(''); $("#txtPhone").val(''); window.location.reload(); } }; //刪除功能
function delBackFillInfo(Delid) { var parm = "delCarsInfo"; $.messager.confirm('系統提示', '您確定要刪除該信息嗎?', function(r) { if (r) { jQuery.ajax({ type: "get", url: "AddTransCarManager.aspx?method=" + parm, r: Math.random(), contentType: "application/json; charset=UTF-8", dataType: "jsonp", data: { id: Delid }, success: msg() }); function msg() { $("#txtCompanyName").val(''); $("#txtCarLicence").val(''); $("#txtLinkMan").val(''); $("#txtPhone").val(''); window.location.reload(); } } }); } </script>

2.C#后台代碼

 protected void Page_Load(object sender, EventArgs e)
        {
            this.Pager1.CurrentDataGrid = this.DataGrid1;
            this.Pager1.DataSourceMethod = new GetDataSourceMethod(GetDataSet);
            this.Pager1.IsAllItem = true;

            string method = Request.QueryString["method"];
            if (method == null)
            {
                return;
            }
            switch (method)
            { 
                case "addCarsInfo"://編輯

                    if (!string.IsNullOrEmpty(Request.QueryString["CompanyName"].ToString()))
                    {
                         string CompanyName = Server.UrlDecode(Request.QueryString["CompanyName"].ToString().Trim());
                         string CID= TransCar.GetComID(CompanyName);
                         if (!string.IsNullOrEmpty(CID))
                         {
                             car.CompanyId = CID;
                         }
                    }

                    #region "車輛信息"
                   if (Request.QueryString["id"]!=null)
                   {
                    //if (!string.IsNullOrEmpty(Request.QueryString["id"].ToString()) && Convert.ToInt32(Request.QueryString["id"].ToString())!=0)
                    //{
                        car.Zt_car_Id = Convert.ToInt32(Request.QueryString["id"].ToString());
                    }
                    else
                    {
                        car.Zt_car_Id = 0;
                    }
                    if (!string.IsNullOrEmpty(Request["CarLicence"].ToString()))
                    {
                        car.CarLicence = Server.UrlDecode(Request["CarLicence"].ToString().Trim());
                    }
                    else
                    {
                        car.CarLicence = "";
                    }
                    if (!string.IsNullOrEmpty(Request["LinkMan"].ToString()))
                    {
                        car.LinkMan = Server.UrlDecode(Request["LinkMan"].ToString().Trim());
                    }
                    else
                    {
                        car.LinkMan = "";
                    }
                    if (!string.IsNullOrEmpty(Request["Phone"].ToString()))
                    {
                        car.Phone = Server.UrlDecode(Request["Phone"].ToString().Trim());
                    }
                    else
                    {
                        car.Phone = "";
                    }
                    if (!string.IsNullOrEmpty(Request["Transport_type"].ToString()))
                    {
                        car.Transport_type = Server.UrlDecode(Request["Transport_type"].ToString().Trim());
                    }
                    else
                    {
                        car.Transport_type = "";
                    }
                    if (!string.IsNullOrEmpty(Request["isDelCar"].ToString()))
                    {
                        car.IsDel = Convert.ToInt32(Request["isDelCar"].ToString());
                    }
                    car.Cid = "";
                    #endregion

                    count_car = bf.AddCarInfo(car);

                    if (count_car > 0)
                    {
                        Page.RegisterStartupScript("message", "<script>alert('保存成功!');</script>");
                    }
                    break;

                case "delCarsInfo"://刪除
                    if (Request.QueryString["id"] != null)
                    {
                        car.Zt_car_Id = Convert.ToInt32(Request.QueryString["id"].ToString());
                        count_car = bf.DelCarsInfo(car.Zt_car_Id);
                    }
                    break;
                default:
                    return;
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM