asp.net(C#) 常用代码
2007-11-25 16:16
399 查看
做 ASP.NET 程序的时候,常常要重复许多代码,如果把它们抽取出来,能极大的提高开发效率,整理了一些自己常用的代码,算法贴出来,以后再补充。
1 using System; 2 using System.Web.Security; 3 4 /// <summary> 5 /// 6 /// ** asp.net(C#) 常用代码 ** 7 /// 8 /// File: Common.cs 9 /// 10 /// Author: 周振兴 (Zxjay 飘遥) 11 /// 12 /// E-Mail: tda7264@163.com 13 /// 14 /// Date: 07-05-19 15 /// 16 /// http://www.cnblogs.com/zxjay 17 /// 18 /// </summary> 19 public class Common 20 { 21 private static string CurrentUser = string.Empty; 22 23 /// <summary> 24 /// ASP.NET 常用算法 25 /// </summary> 26 public Common() 27 { 28 } 29 30 /// <summary> 31 /// 获取或设置当前用户名称 32 /// </summary> 33 public static string GetCurrentUser 34 { 35 set { CurrentUser = value; } 36 get { return CurrentUser; } 37 } 38 39 /// <summary> 40 /// 限制字符串长度,清除特殊字符 41 /// </summary> 42 /// <param name="str"> 待处理的字符串 </param> 43 /// <param name="maxlength"> 最大长度 </param> 44 /// <returns> 处理后的字符串 </returns> 45 public static string ClearString(string str,int maxlength) 46 { 47 if (string.IsNullOrEmpty(str)) 48 { 49 return string.Empty; 50 } 51 str=str.Trim(); 52 if(str==string.Empty) 53 { 54 return string.Empty; 55 } 56 if(str.Length>maxlength) 57 { 58 str=str.Substring(0,maxlength); 59 } 60 str=str.Replace("'","''"); 61 return str; 62 } 63 64 /// <summary> 65 /// 密码字符串散列加密( MD5算法 ) 66 /// </summary> 67 /// <param name="inputstr"> 待加密的字符串 </param> 68 /// <returns> 加密后字符串 </returns> 69 public static string MD5(string inputstr) 70 { 71 return FormsAuthentication.HashPasswordForStoringInConfigFile (inputstr, "MD5").ToLower().Substring(0, 15); 72 } 73 74 /// <summary> 75 /// 插入Flash或图像 76 /// </summary> 77 /// <param name="filename"> 媒体文件名称 </param> 78 /// <param name="width"> 宽度 </param> 79 /// <param name="height"> 高度 </param> 80 /// <returns> 能展示媒体的 HTML 代码 </returns> 81 public static string ShowMedia(string filename, int width, int height) 82 { 83 string fname = filename.Trim(); 84 if (filename == string.Empty) 85 { 86 return string.Empty; 87 } 88 89 string fileext = fname.Substring(fname.LastIndexOf(".")); 90 //flash文件 91 if (fileext.ToLower() == ".swf") 92 { 93 string swfObject = @"<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='" + width + @"' height='" + height + @"'> 94 <param name='movie' value='" + filename + @"' /> 95 <param name='quality' value='high' /> 96 <param name='wmode' value='transparent'> 97 <embed src='" + filename + @"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='" + width + @"' height='" + height + @"'></embed> 98 </object> "; 99 return swfObject; 100 } 101 //图像文件 102 else 103 { 104 string imgObject = @"<img alt='' width='" + width + @"px' height='" + height + @"px' src='" + filename + @"' />"; 105 return imgObject; 106 } 107 } 108 109 /// <summary> 110 /// 弹出提示框 111 /// </summary> 112 /// <param name="message"> 提示信息 </param> 113 /// <returns> 提示框的JS代码 </returns> 114 public static string ShowMessage(string message) 115 { 116 return "<script>alert('" + message + "');</script>"; 117 } 118 119 /// <summary> 120 /// 弹出提示框并且重定向到另一链接 121 /// </summary> 122 /// <param name="message"> 提示信息 </param> 123 /// <param name="urlToRedirect"> 重定向到的链接 </param> 124 /// <returns> 提示框的JS代码 </returns> 125 public static string ShowMessageAndRedirect(string message, string urlToRedirect) 126 { 127 return "<script>alert('" + message + "');document.location.href='" + urlToRedirect + "'</script>"; 128 } 129 130 /// <summary> 131 /// 打开可调节大小的窗口 132 /// </summary> 133 /// <param name="url"> 待打开的链接 </param> 134 /// <param name="width"> 宽度 </param> 135 /// <param name="height"> 高度 </param> 136 /// <param name="locationX"> 距屏幕左端的距离 </param> 137 /// <param name="locationY"> 据屏幕上端的距离 </param> 138 /// <returns> 可调节大小的窗口JS代码 </returns> 139 public static string OpenResizableWindow(string url, int width, int height, int locationX, int locationY) 140 { 141 string jsString = @"width={0}, height={1}, left={2}, top={3},location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, titlebar=no, toolbar=no, directories=no"; 142 return OpenWindow(url, string.Format(jsString, width, height, locationX, locationY)); 143 } 144 145 /// <summary> 146 /// 打开固定大小的窗口 147 /// </summary> 148 /// <param name="url"> 待打开的链接 </param> 149 /// <param name="width"> 宽度 </param> 150 /// <param name="height"> 高度 </param> 151 /// <param name="locationX"> 距屏幕左端的距离 </param> 152 /// <param name="locationY"> 据屏幕上端的距离 </param> 153 /// <returns> 固定大小的窗口JS代码 </returns> 154 public static string OpenFixedWindow(string url, int width, int height, int locationX, int locationY) 155 { 156 string jsString = @"width={0}, height={1}, left={2}, top={3},location=no, menubar=no, resizable=no, scrollbars=no, status=no, titlebar=no, toolbar=no, directories=no"; 157 return OpenWindow(url, string.Format(jsString, width, height, locationX, locationY)); 158 } 159 160 /// <summary> 161 /// 打开窗口 162 /// </summary> 163 /// <param name="url"> 待打开的链接 </param> 164 /// <param name="sFeatures"> 定义窗口格式 </param> 165 /// <returns> 打开窗口的JS代码 </returns> 166 public static string OpenWindow(string url, string sFeatures) 167 { 168 string jsString = @"<script type='text/javascript'>" + 169 @"window.open('{0}','','{1}');"+ 170 @"</script>"; 171 return string.Format(jsString, url, sFeatures); 172 } 173 174 /// <summary> 175 /// 打开可调节大小的模态对话框 176 /// </summary> 177 /// <param name="url"> 待打开的链接 </param> 178 /// <param name="width"> 模态对话框宽度 </param> 179 /// <param name="height"> 模态对话框高度 </param> 180 /// <returns> 模态对话框的JS代码 </returns> 181 public static string ShowResizableModalDialog(string url, int width, int height) 182 { 183 string sFeatures = "dialogWidth:{0}px;dialogHeight:{1}px;center:yes; resizable:yes;status:yes;scroll:yes;edge:sunken; "; 184 return ShowModalDialog(url, string.Format(sFeatures, width, height)); 185 } 186 187 /// <summary> 188 /// 打开固定大小的模态对话框 189 /// </summary> 190 /// <param name="url"> 待打开的链接 </param> 191 /// <param name="width"> 模态对话框宽度 </param> 192 /// <param name="height"> 模态对话框高度 </param> 193 /// <returns> 模态对话框的JS代码 </returns> 194 public static string ShowFixedModalDialog(string url, int width, int height) 195 { 196 string sFeatures = "dialogWidth:{0}px;dialogHeight:{1}px;center:yes; resizable:no;status:no;scroll:no;"; 197 return ShowModalDialog(url, string.Format(sFeatures, width, height)); 198 } 199 200 /// <summary> 201 /// 打开模式对话框 202 /// </summary> 203 /// <param name="url"> 待打开的链接 </param> 204 /// <param name="sFeatures"> 定义模态对话框格式 </param> 205 /// <returns> 模态对话框的JS代码 </returns> 206 public static string ShowModalDialog(string url, string sFeatures) 207 { 208 string jsString = @"<script type='text/javascript'>" + 209 @"showModalDialog('{0}','','{1}'" + 210 @"</script>"; 211 return string.Format(jsString, url, sFeatures); 212 } 213 214 215 } 216
相关文章推荐
- C# ASP.NET 最常用的通用权限的3个方法例子展示(每个功能一行代码实现)
- ASP.NET(C#)常用代码30例
- [转]ASP.NET(C#)常用代码30例
- [转]ASP.NET(C#)常用代码30例
- C# ASP.NET 最常用的通用权限的3个方法例子展示(每个功能一行代码实现)
- ASP.NET(C#)程序中常用的三十三种代码
- ASP.NET(C#)常用代码30例
- ASP.NET(C#)常用代码30例
- ASP.NET(C#)常用代码30例
- ASP.NET常用到的js通用函数 c#代码可以做成dll来使用
- ASP.NET(C#)常用代码30例
- ASP.NET(C#)常用代码30例
- ASP.NET常用代码(c#)
- ASP.net和C#常用代码收集(不断添加ing)
- [转帖] ASP.NET(C#)常用代码30例
- ASP.NET(C#)程序中常用的三十三种代码
- C# ASP.NET 最常用的通用权限的3个方法例子展示(每个功能一行代码实现)
- ASP.NET(C#)常用代码30例
- ASP.NET中常用的三十三种代码(C#版)
- ASP.NET中常用的三十三种代码(C#版)