返回> 网站首页
[转载]ASP.net通用方法
yoours2014-04-23 13:18:26
简介一边听听音乐,一边写写文章。
JavaScript Code
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | public class FormatStr { #region MD5加密 /// <summary> /// MD5加密操作 /// </summary> /// <param name="str">加密的字符串</param> /// <returns></returns> public static string MD5(string str) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5"); } #endregion #region 去除HTML标记 /// <summary> /// 去除HTML标记 /// </summary> /// <param name="NoHTML">包括HTML的源码 </param> /// <returns>已经去除后的文字</returns> public static string NoHTML(string Htmlstring) { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase); //删除HTML Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase); Htmlstring.Replace("<", ""); Htmlstring.Replace(">", ""); Htmlstring.Replace("\r\n", ""); Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim(); return Htmlstring; } #endregion #region 去除非法字串 /// <summary> /// 去除非法字串 /// </summary> /// <param name="strChar">原字串</param> /// <returns>过滤过的字串</returns> public static string ReplaceBadChar(string strChar) { if (strChar.Trim() == "") { return ""; } else { strChar = strChar.Replace("'", ""); strChar = strChar.Replace("*", ""); strChar = strChar.Replace("?", ""); strChar = strChar.Replace("(", ""); strChar = strChar.Replace(")", ""); strChar = strChar.Replace("<", ""); strChar = strChar.Replace("=", ""); return strChar.Trim(); } } #endregion #region 检察是否都是数字 /// <summary> /// 检察是否都是数字 /// </summary> /// <param name="str">要检查的字串</param> /// <returns>bool</returns> public static bool IsNumeric(string str) { Regex reg = new Regex(@"^[+]?\d*$"); return reg.IsMatch(str); } #endregion #region 检察是否正确的Email格式 /// <summary> /// 检察是否正确的Email格式 /// </summary> /// <param name="str">要检查的字串</param> /// <returns>bool</returns> public static bool IsEmail(string str) { Regex reg = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); return reg.IsMatch(str); } #endregion #region 检察是否正确的日期格式 /// <summary> /// 检察是否正确的日期格式 /// </summary> /// <param name="str">要检查的字串</param> /// <returns>bool</returns> public static bool IsDate(string str) { //考虑到了4年一度的366天,还有特殊的2月的日期 Regex reg = new Regex(@"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$"); return reg.IsMatch(str); } #endregion #region HTML转换为字符串 /// <summary> /// HTML转换为字符串,转换标识 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string htmToStr(string str) { return str.Replace("\'", "mySQ;").Replace("\"", "myDQ;").Replace("<", "myLt;").Replace(">", "myGt;").Replace("-", "myMl;").Replace("_", "myBl;").Replace("%", "myBs;").Replace("?", "myQe;").Replace("*", "myAl;"); } #endregion #region 字符串转换为HTML /// <summary> /// 字符串转换为HTML,将还原标识 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string strToHtm(string str) { if (str == null) return ""; return str.Replace("mySQ;", "\'").Replace("myDQ;", "\"").Replace("myLt;", "<").Replace("myGt;", ">").Replace("myMl;", "-").Replace("myBl;", "_").Replace("myBs;", "%").Replace("myQe;", "?").Replace("myAl;", "*"); } #endregion #region 文本转换为HTML /// <summary> /// 文本转换为HTML,更换(\n) /// </summary> /// <param name="str"></param> /// <returns></returns> public static string textToHtm(string str) { return str.Replace("\t", " ").Replace("<", "<").Replace(">", ">").Replace(" ", " ").Replace("\r\n", "<br>").Replace("\n", "<br>"); } #endregion #region 文本转换为输入框内容 /// <summary> /// 文本转换为输入框内容, /// </summary> /// <param name="str"></param> /// <returns></returns> public static string HtmToInputValue(string str) { //return str.Replace("<","<").Replace(">",">").Replace("\"",""").Replace("&","&"); return str.Replace("\"", """); } #endregion #region 防Sql注入攻击 /// <summary> /// 防Sql注入攻击 /// </summary> /// <param name="strQueryName"></param> /// <returns></returns> public static string getQueryString(string strQueryName) { //(')(--)(or )(=) System.Web.HttpRequest myRequest = System.Web.HttpContext.Current.Request; string str1 = myRequest.QueryString[strQueryName]; if (str1 == null) return ""; str1 = str1.Replace("'", "’").Replace("--", "—").Replace("=", "=").Replace("or", "o r").Replace("Or", "O r").Replace("OR", "O R").Replace("oR", "o R").Replace(">", "〉").Replace("<", "〈"); return str1; } #endregion #region 防Sql注入攻击 /// <summary> /// 防Sql注入攻击 /// </summary> /// <param name="strQueryName"></param> /// <returns></returns> public static string getSQLString(string strQueryName) { if (strQueryName == null) return ""; strQueryName = strQueryName.Replace("'", "’").Replace("--", "—").Replace("=", "=").Replace("or", "o r").Replace("Or", "O r").Replace("OR", "O R").Replace("oR", "o R").Replace(">", "〉").Replace("<", "〈"); return strQueryName; } #endregion } // 取汉字首字母 static public string GetChineseSpell(string strText) { int len = strText.Length; string myStr = ""; for(int i=0;i<len;i++) { myStr += getSpell(strText.Substring(i,1)); } return myStr; } static public string getSpell(string cnChar) { byte[] arrCN = Encoding.Default.GetBytes(cnChar); if(arrCN.Length > 1) { int area = (short)arrCN[0]; int pos = (short)arrCN[1]; int code = (area<<8) + pos; int[] areacode = {45217,45253,45761,46318,46826,47010,47297,47614,48119,48119,49062,49324,49896,50371,50614,50622,50906,51387,51446,52218,52698,52698,52698,52980,53689,54481}; for(int i=0;i<26;i++) { int max = 55290; if(i != 25) max = areacode[i+1]; if(areacode[i]<=code && code<max) { return Encoding.Default.GetString(new byte[]{(byte)(65+i)}); } } return "*"; }else return cnChar; } // DropDownList手动构造一个下拉树形 private void bindDDL(DropDownList DDL, DataSet ds, int ParentID, string Tab) { DataView dv = new DataView(ds.Tables[0]); dv.RowFilter = "ParentId=" + ParentID.ToString(); int count = 0; foreach (DataRowView Row in dv) { count++; if (Tab.IndexOf("├") > -1) { Tab = Tab.Replace("├", "│"); } if (Tab.IndexOf("└") > -1) { Tab = Tab.Replace("└", ""); } if (dv.Count == count) { Tab = Tab + "└"; } else { Tab = Tab + "├"; } dropMenuML.Items.Add(new ListItem(Tab + Row["MenuName"].ToString(), Row["ID"].ToString())); bindDDL(dropMenuML, ds, Convert.ToInt32(Row["ID"].ToString()), Tab); Tab = Tab.Remove(Tab.Length - 1, 1); } } private void ShowData() { this.dropMenuML.DataTextField = "MenuName"; this.dropMenuML.DataValueField = "ID"; MenuBll bll=new MenuBll(); DataSet ds = bll.GetList("MenuName,ID,ParentId,Url", "MenuManage", ""); //GetList就是一个查询列表select * from MenuManage; bindDDL(dropMenuML, ds, 0, ""); dropMenuML.Items.Add(new ListItem("----Please Choose----", "0")); dropMenuML.SelectedIndex = dropMenuML.Items.Count - 1; } // 全选/全不选 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> function selectAll(checkbox) { $('input[type=checkbox]').attr('checked', $(checkbox).attr('checked')); } </script> </head> <body> <form id="form1" runat="server"> <div> <input type="checkbox" onclick="selectAll(this);" />全选<br/> <input type="checkbox" /><br/> <input type="checkbox" /><br/> <input type="checkbox" /><br/> <input type="checkbox" /><br/> <input type="checkbox" /><br/> <input type="checkbox" /><br/> </div> </form> </body> </html> 解析Json和封装Json using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization namespace Monitor { /// 1.将List<T>类型的数据转换为JSON格式 /// 2.将T类型对象转换为JSON格式对象 /// 3.将JSON格式对象转换为T类型对象 public static class JsonHelper { /// <summary> /// 转换对象为JSON格式数据 /// </summary> /// <typeparam name="T">类</typeparam> /// <param name="obj">对象</param> /// <returns>字符格式的JSON数据</returns> public static string GetJSON<T>(object obj) { string result = String.Empty; try { System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { serializer.WriteObject(ms, obj); result = System.Text.Encoding.UTF8.GetString(ms.ToArray()); } } catch (Exception ex) { throw ex; } return result; } /// <summary> /// 转换List<T>的数据为JSON格式 /// </summary> /// <typeparam name="T">类</typeparam> /// <param name="vals">列表值</param> /// <returns>JSON格式数据</returns> public static string JSON<T>(List<T> vals) { System.Text.StringBuilder st = new System.Text.StringBuilder(); try { System.Runtime.Serialization.Json.DataContractJsonSerializer s = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); foreach (T city in vals) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { s.WriteObject(ms, city); st.Append(System.Text.Encoding.UTF8.GetString(ms.ToArray())); } } } catch (Exception ex) { throw ex; } return st.ToString(); } /// <summary> /// JSON格式字符转换为T类型的对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonStr"></param> /// <returns></returns> public static T ParseFormByJson<T>(string jsonStr) { T obj = Activator.CreateInstance<T>(); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonStr))) { System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T)); return (T)serializer.ReadObject(ms); } } } } 调用 Object foo = JsonHelper.ParseFormByJson<Online>(json); |
文章评论
1791人参与,0条评论