系列索引
目录
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;ListRules = 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.前端的全部代码