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; } }