免费预览已结束,剩余2页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
利用HttpHandler和Jquery进行数据库增改删查操作 l 引言本文一步步教你如何在A和Jquery Ajax API中用HttpHandler实现数据库增改删查的基本操作。为了简单起见,本文不涉及表单验证和应用程序界面设计方面的知识。l 必备条件在前面我提及已经通过Jquery Ajax API调用HttpHandler,因此需要在Web项目中添加jquery文件的引用,官方网址:,如果使用vs2010开发,那么jquery文件默认包含在新建的Web项目中。l 实现首先在visual studio中新建Web项目,然后新建一个名为Script的文件夹,并添加一个名为Commonfunction.js空白js文件。l 数据表设计在Sqlserver数据库中新建如下所示的Products数据表,并新建Products相关的数据操作类,代码如下:View Code然后新建名为JsonResponse类,用来将从数据库中接收到的数据序列化成Json格式,代码如下:View Code using System;using System.Collections.Generic;using System.Linq;using System.Web;public class JsonResponse private bool _IsSucess = false; public bool IsSucess get return _IsSucess; set _IsSucess = value; private string _Message = string.Empty; public string Message get return _Message; set _Message = value; private object _ResponseData = null; public object ResponseData get return _ResponseData; set _ResponseData = value; private string _CallBack = string.Empty; public string CallBack get return _CallBack; set _CallBack = value; 首页Default.aspx中Product信息的相关html代码如下,主要用来提交保存单条产品信息。View Code在页面的head标签中添加如下引用: 新建ProductList.ashx,主要处理客户端提交的ajax数据请求,代码如下:View Code public class ProductList : IHttpHandler string MethodName = string.Empty; string CallBackMethodName = string.Empty; object Parameter = string.Empty; DbProducts _DbProducts = new DbProducts(); public void ProcessRequest(HttpContext context) context.Response.ContentType = application/x-javascript; MethodName = context.Request.Paramsmethod; Parameter = context.Request.Paramsparam; CallBackMethodName = context.Request.Paramscallbackmethod; switch (MethodName.ToLower() case getproducts: context.Response.Write(GetDetails(); break; case getbyid: context.Response.Write(GetById(); break; case insert: context.Response.Write(Insert(context); break; case update: context.Response.Write(Update(context); break; case delete: context.Response.Write(Delete(); break; public string GetDetails() JsonResponse _response = new JsonResponse(); System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer(); try System.Collections.Generic.List _Products = _DbProducts.GetProductDetails(); _response.IsSucess = true; _response.Message = string.Empty; _response.CallBack = CallBackMethodName; _response.ResponseData = _Products; catch (Exception ex) _response.Message = ex.Message; _response.IsSucess = false; return jSearializer.Serialize(_response); public string GetById() JsonResponse _response = new JsonResponse(); System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer(); try Product _Products = _DbProducts.GetProductById(Convert.ToInt32(Parameter); _response.IsSucess = true; _response.Message = string.Empty; _response.CallBack = CallBackMethodName; _response.ResponseData = _Products; catch (Exception ex) _response.Message = ex.Message; _response.IsSucess = false; return jSearializer.Serialize(_response); public string Insert(HttpContext context) JsonResponse _response = new JsonResponse(); System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer(); try Product _P = new Product(); _P.Name = context.Request.Paramsname.ToString(); _P.Unit = context.Request.Paramsunit.ToString(); _P.Qty = Convert.ToDecimal(context.Request.ParamsQty.ToString(); _response.IsSucess = true; _response.CallBack = CallBackMethodName; _response.ResponseData = _DbProducts.InsertProduct(_P); _response.Message = SucessFully Saved; catch (Exception ex) _response.Message = ex.Message; _response.IsSucess = false; return jSearializer.Serialize(_response); public string Update(HttpContext context) JsonResponse _response = new JsonResponse(); System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer(); try Product _P = new Product(); _P.Name = context.Request.Paramsname.ToString(); _P.Unit = context.Request.Paramsunit.ToString(); _P.Qty = Convert.ToDecimal(context.Request.ParamsQty.ToString(); _P.ProductID = Convert.ToInt32 (context.Request.ParamsProductID.ToString(); _response.IsSucess = true; _response.Message = SucessFully Updated; _response.CallBack = CallBackMethodName; _response.ResponseData = _DbProducts.UpdateProduct(_P); catch (Exception ex) _response.Message = ex.Message; _response.IsSucess = false; return jSearializer.Serialize(_response); public string Delete() JsonResponse _response = new JsonResponse(); System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer(); try _response.IsSucess = true; _response.Message = Record Successfully Deleted; _response.CallBack = CallBackMethodName; _response.ResponseData = _DbProducts.DeleteProduct(Convert.ToInt32(Parameter); catch (Exception ex) _response.Message = ex.Message; _response.IsSucess = false; return jSearializer.Serialize(_response); public bool IsReusable get return false; 在Coomonfunction.js中添加公用函数DoAjaxCall,该函数利用Ajax调用HttpHandler处理结果,并通过methodname,datatoinsert,callbackfunctionname,datatype和data等传入的参数,来告知服务器端执行的操作(增改删查)并返回服务器端处理成功或失败的消息。View Code保存按钮的客户端事件SaveProducts()调用和定义如下,其中通过判断ProductID是否为0来判断当前的操作是插入还是更新产品信息。View Code function SaveProducts() var Param = name= + document.getElementById(txtName).value + &unit= + document.getElementById(txtUnit).value + &Qty= + document.getElementById(txtQty).value; if (ProductID = 0) DoAjaxCall(?method=Insert&callbackmethod=InsertProductSucess, s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农村道路改造提升项目初步设计
- 综合管廊防水施工专项方案
- 2026年肾内科规培模拟试卷及答案
- 农产品集散中心项目节能评估报告
- 隧道工程施工专项方案
- 易涝点排水系统改造工程设计方案
- 2026年危化品模拟试卷及答案
- 2026年运输车驾驶员安全教育培训考核模拟试题带答案
- 2026年河南护士模拟试卷带答案
- 2026年微课开发模拟试卷及答案
- 2026中国邮政联合校园招聘笔试参考题库(含答案解析)
- (一模)嘉兴市2026-2027学年第一学期高三教学质量检测语文试卷(含答案)
- 2026中国洗衣干衣行业市场现状需求分析投资布局发展策略研究文献
- 2027年湖北交通投资集团有限公司秋季校园招聘131人笔试备考题库及答案解析
- 2026山东青岛水务集团有限公司招聘笔试历年参考题库附带答案详解
- 2026年党支部基层总结报告
- T∕CCEAS008-2026 建设工程造价咨询成果文件质量标准
- 2018 机械通风冷却塔 第1部分:中小型开式冷却塔
- 中国膀胱癌诊疗指南2025版
- 梅尼埃病前庭功能检测
- 凉山2025年四川凉山州雷波重点国有林保护局招聘27人笔试历年参考题库附带答案详解
评论
0/150
提交评论