博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Web jquery表格组件 JQGrid 的使用 - 全部代码
阅读量:6038 次
发布时间:2019-06-20

本文共 5822 字,大约阅读时间需要 19 分钟。

 

系列索引

 

 

目录

 

 

9.http handler 里的全部代码  

public class UserHandler : IHttpHandler{public void ProcessRequest(HttpContext context){//查找if (context.Request.Params.Get("_search") == "true"){string sfilters = context.Request.Params.Get("filters");context.Response.Write(GetJson(SearchUsersDT(sfilters)));return;}NameValueCollection forms = context.Request.Form;string strOperation = forms.Get("oper");string strResponse = string.Empty;if (strOperation == null){//排序if (context.Request.Params.Get("sidx") != null &&!string.IsNullOrEmpty(context.Request.Params.Get("sidx").ToString()) &&context.Request.Params.Get("sord") != null &&!string.IsNullOrEmpty(context.Request.Params.Get("sord").ToString())){context.Response.Write(GetJson(GetUserDTSorted(context.Request.Params.Get("sidx").ToString(), context.Request.Params.Get("sord").ToString())));return;}strResponse = GetJson(GetUserDT()); //load data}else if (strOperation == "del"){strResponse = DeleteUser(forms.Get("delId").ToString()) ? "删除成功!" :"删除失败,请确认!";}else{AddEdit(forms, out strResponse);}context.Response.Write(strResponse);}private void AddEdit(NameValueCollection forms, out string strResponse){string strOperation = forms.Get("oper");string strEmpId = string.Empty;User user = new User();user.UserCode = forms.Get("UserCode").ToString();user.Password = forms.Get("Password").ToString();string sTmp = string.Empty;if (strOperation == "add"){if (CheckUserExist(user.UserCode, "")){sTmp = "用户名重复,请确认!";}else{sTmp = AddUser(user) ? "用户添加成功!" : "用户添加失败,请确认!";}}else if (strOperation == "edit"){user.UserId = int.Parse(forms.Get("UserId").ToString());if (CheckUserExist(user.UserCode, user.UserId.ToString())){sTmp = "用户名重复,请确认!";}else{sTmp = UpdateUser(user) ? "用户更新成功!" : "用户更新失败,请确认!";}}strResponse = sTmp;}public bool IsReusable{get{return false;}}private DataTable GetUserDT(){string cmdText = "SELECT UserId, UserCode, Password FROM T_User";SQLHelper sqlhelper = new SQLHelper();DataTable dt = sqlhelper.Selectinfo(cmdText);return dt;}private string GetJson(DataTable dt){JavaScriptSerializer serializer = new JavaScriptSerializer();List
> rows = new List
>();Dictionary
row = null;foreach (DataRow dr in dt.Rows){row = new Dictionary
();foreach (DataColumn col in dt.Columns){string s = dr[col].ToString(); //特殊情况if (col.ColumnName == "IsAdmin"){s = s == "1" ? "是" : "否";}if (col.ColumnName == "HasWrite"){s = s == "1" ? "是" : "否";}row.Add(col.ColumnName.Trim(), s);}rows.Add(row);}return serializer.Serialize(rows);}///
/// 根据jqgrid的查询操作符和字段拼接sql语句/// ///
jqgrid的查询操作符///
sql wehere语句
///
查询字段名称private string GetSQLOperater(string op, string field){string s = string.Empty;switch (op){case "eq": return field + " = @" + field;//等于case "ne": return field + " <> @" + field;//不等于case "bw": return field + " like @" + field + "'%'"; //开始于case "bn": return field + " not like @" + field + "'%'"; //不开始于case "ew": return field + " like '%' + @" + field; //结束于case "en": return field + " not like '%' + @" + field; //不结束于case "cn": return field + " like + '%' + " + field + "'%'"; //包含case "nc": return field + " not like + '%' + @" + field + "'%'"; //不包含case "nu": return "(" + field + " = '' or is null)"; //空值case "nn": return "(" + field + " <> '' or is not null)"; //非空值case "in": return ""; //属于case "ni": return ""; //不属于default: return "";}}private DataTable SearchUsersDT(string filters){string jsonRes = string.Empty;System.Runtime.Serialization.Json.DataContractJsonSerializer json = newSystem.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(GridSearch));using (MemoryStream stream = newMemoryStream(System.Text.Encoding.UTF8.GetBytes(filters))){GridSearch gridSearch = (GridSearch)json.ReadObject(stream);string groupOp = gridSearch.groupOp;List
Rules = gridSearch.rules;string sql = "select UserId, UserCode, Password FROM T_User";MySqlParameter[] paras = new MySqlParameter[Rules.Count];bool bFirst = true;for (int i = 0; i < Rules.Count; i++){GridSearchRules r = Rules[i];string field = r.field;string op = r.op;string data = r.data;sql = bFirst ? sql + " where " + GetSQLOperater(op, field) : sql +groupOp + GetSQLOperater(op, field);paras[i] = new MySqlParameter("@" + field, data);}SQLHelper sqlhelper = new SQLHelper();DataTable dt = sqlhelper.Selectinfo(sql);return dt;}}private bool DeleteUser(string userId){bool flag = true;string[] idlist = userId.Split(',');foreach (var sid in idlist){string sql = "DELETE FROM T_User WHERE UserId=" + userId;SQLHelper sqlhelper = new SQLHelper();flag = sqlhelper.AddDelUpdate(sql) > 0;}return flag;}private bool AddUser(User objuser){bool flag = false;string cmdText = "INSERT INTO T_User (UserCode,Password) VALUES ('" +objuser.UserCode + "','" + objuser.Password + "')";try{SQLHelper sqlhelper = new SQLHelper();flag = sqlhelper.AddDelUpdate(cmdText) > 0;}catch (Exception ex){throw ex;}return flag;}///
/// 更新用户信息/// ///
///
更新成功与否
private bool UpdateUser(User objuser){string sql = "UPDATE T_User SET UserCode = '" + objuser.UserCode +"',Password = '" + objuser.Password + "' WHERE UserId=" + objuser.UserId;SQLHelper sqlhelper = new SQLHelper();return sqlhelper.AddDelUpdate(sql) > 0;}private DataTable GetUserDTSorted(string field, string oper){string cmdText = "SELECT UserId, UserCode, UserName, Password, RoleId,CreateBy, CreateTime FROM T_User order by " + field + " " + oper;SQLHelper sqlhelper = new SQLHelper();DataTable dt = sqlhelper.Selectinfo(cmdText);return dt;}private bool CheckUserExist(string UserCode, string UserId){string sql = "select * from T_User where UserCode = '" + UserCode + "' andUserId <> " + UserId;SQLHelper sqlhelper = new SQLHelper();DataTable dt = sqlhelper.Selectinfo(sql);return dt == null || (dt != null && dt.Rows.Count == 1);}}

 

10.前端的全部代码

 

转载地址:http://yprhx.baihongyu.com/

你可能感兴趣的文章
Sublime 中运行 Shell 、Python、Lua、Groovy...等各种脚本
查看>>
【Java集合源码剖析】ArrayList源码剖析
查看>>
linux的目录结构
查看>>
这次逻辑通了,
查看>>
HTMLHelper
查看>>
快速构建Windows 8风格应用29-捕获图片与视频
查看>>
OC语言Block和协议
查看>>
使用xpath时出现noDefClass的错误(找不到某个类)
查看>>
.Net规则引擎介绍 - REngine
查看>>
CSS3 transforms 3D翻开
查看>>
利用传入的Type类型来调用范型方法的解决方案
查看>>
Top命令内存占用剖析
查看>>
转 网络IO模型:同步IO和异步IO,阻塞IO和非阻塞IO
查看>>
求带分数(蓝桥杯)
查看>>
Bootstrap系列 -- 11. 基础表单
查看>>
Retrofit 入门学习
查看>>
Spring Boot学习笔记
查看>>
python3存入redis是bytes
查看>>
laravel 集合接口
查看>>
C/C++二进制读写png文件
查看>>