asp.net几个是用函数

本文提供了一系列实用的C#代码片段,包括获取用户IP、字符串处理、HTML编码与解码、生成随机数、弹窗提示等功能。此外还介绍了如何通过URL获取网页内容、MD5加密方法及导出GridView到Excel的技术。
  1. /// <summary>   
  2. /// 得到站点用户IP   
  3. /// </summary>   
  4. /// <returns></returns>   
  5. public   static   string  getUserIP()   
  6. {   
  7.      return  HttpContext.Current.Request.ServerVariables[ "REMOTE_ADDR" ].ToString();   
  8. }   
  9.   
  10. /// <summary>   
  11. /// 去除字符串最后一个','号   
  12. /// </summary>   
  13. /// <param name="chr">:要做处理的字符串</param>   
  14. /// <returns>返回已处理的字符串</returns>   
  15. public   static   string  Lost( string  chr)   
  16. {   
  17.      if  (chr ==  null  || chr ==  string .Empty)   
  18.     {   
  19.          return   "" ;   
  20.     }   
  21.      else   
  22.     {   
  23.         chr = chr.Remove(chr.LastIndexOf( "," ));   
  24.          return  chr;   
  25.     }   
  26. }   
  27.   
  28. /// <summary>   
  29. /// 去除字符串第一个'/'号   
  30. /// </summary>   
  31. /// <param name="chr">要做处理的字符串</param>   
  32. /// <returns>返回已处理的字符串</returns>   
  33. public   static   string  lostfirst( string  chr)   
  34. {   
  35.      string  flg =  "" ;   
  36.      if  (chr !=  string .Empty || chr !=  null )   
  37.     {   
  38.          if  (chr.Substring(0, 1) ==  "/" )   
  39.             flg = chr.Replace(chr.Substring(0, 1),  "" );   
  40.          else   
  41.             flg = chr;   
  42.     }   
  43.      return  flg;   
  44. }   
  45.   
  46. /// <summary>   
  47. /// 替换html中的特殊字符   
  48. /// </summary>   
  49. /// <param name="theString">需要进行替换的文本。</param>   
  50. /// <returns>替换完的文本。</returns>   
  51. public   static   string  HtmlEncode( string  theString)   
  52. {   
  53.     theString = theString.Replace( ">"">" );   
  54.     theString = theString.Replace( "<""<" );   
  55.     theString = theString.Replace( "  ""  " );   
  56.     theString = theString.Replace( "  ""  " );   
  57.     theString = theString.Replace( "/""""" );   
  58.     theString = theString.Replace( "/'""'" );   
  59.     theString = theString.Replace( "/n""<br/> " );   
  60.      return  theString;   
  61. }   
  62.   
  63. /// <summary>   
  64. /// 恢复html中的特殊字符   
  65. /// </summary>   
  66. /// <param name="theString">需要恢复的文本。</param>   
  67. /// <returns>恢复好的文本。</returns>   
  68. public   static   string  HtmlDiscode( string  theString)   
  69. {   
  70.     theString = theString.Replace( ">"">" );   
  71.     theString = theString.Replace( "<""<" );   
  72.     theString = theString.Replace( " "" " );   
  73.     theString = theString.Replace( "  ""  " );   
  74.     theString = theString.Replace( """"/"" );   
  75.     theString = theString.Replace( "'""/'" );   
  76.     theString = theString.Replace( "<br/> ""/n" );   
  77.      return  theString;   
  78. }   
  79.   
  80.   
  81.   
  82. /// <summary>   
  83. /// 生成随机数字   
  84. /// </summary>   
  85. /// <param name="length">生成长度</param>   
  86. /// <returns></returns>   
  87. public   static   string  Number( int  Length)   
  88. {   
  89.      return  Number(Length,  false );   
  90. }   
  91.   
  92. /// <summary>   
  93. /// 生成随机数字   
  94. /// </summary>   
  95. /// <param name="Length">生成长度</param>   
  96. /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>   
  97. /// <returns></returns>   
  98. public   static   string  Number( int  Length,  bool  Sleep)   
  99. {   
  100.      if  (Sleep)   
  101.         System.Threading.Thread.Sleep(3);   
  102.      string  result =  "" ;   
  103.     System.Random random =  new  Random();   
  104.      for  ( int  i = 0; i < Length; i++)   
  105.     {   
  106.         result += random.Next(10).ToString();   
  107.     }   
  108.      return  result;   
  109. }   
  110.   
  111. //弹出对话框   
  112. public   static   void  salert( string  str)   
  113. {   
  114.         HttpContext.Current.Response.Write( "<script>alert('"  + str +  "');</script>" );   
  115. }   
  116.   
  117.   
  118.   
  119.   /// <summary>   
  120.      /// 显示消息提示框,并回到前一页面   
  121.      /// </summary>   
  122.      /// <param name="page">当前页面指针,一般为this</param>   
  123.      /// <param name="strMsg">提示信息</param>   
  124.      public   static   void  ShowGoHistory(System.Web.UI.Page page,  string  strMsg)   
  125.     {   
  126.         page.ClientScript.RegisterStartupScript(page.GetType(),  "message""<script language='javascript' defer>alert('"  + strMsg.ToString() +  "');window.history.go(-1);</script>" );   
  127.     }   
  128.   
  129.      /// <summary>   
  130.      /// 显示消息提示对话框,并进行页面跳转   
  131.      /// </summary>   
  132.      /// <param name="page">当前页面指针,一般为this</param>   
  133.      /// <param name="strMsg">提示信息</param>   
  134.      /// <param name="url"> 跳转的目标URL</param>   
  135.      public   static   void  ShowRedirect(System.Web.UI.Page page,  string  strMsg,  string  url)   
  136.     {   
  137.         StringBuilder Builder =  new  StringBuilder();   
  138.         Builder.Append( "<script language='javascript' defer>" );   
  139.         Builder.AppendFormat( "alert('{0}');" , strMsg);   
  140.         Builder.AppendFormat( "top.location.href='{0}'" , url);   
  141.         Builder.Append( "</script>" );   
  142.         page.ClientScript.RegisterStartupScript(page.GetType(),  "message" , Builder.ToString());   
  143.     }   
  144.   
  145. //为了插入单引号   
  146. public   static   string  delSingle( string  str)   
  147. {   
  148.          return  str.Replace( "'""''" );   
  149.  }   
  150.   
  151. //由gridviw导出为Excel   
  152. public   static   void  ToExcel(System.Web.UI.Control ctl)   
  153. {   
  154.     HttpContext.Current.Response.AppendHeader( "Content-Disposition""attachment;filename=Excel.xls" );   
  155.     HttpContext.Current.Response.Charset =  "UTF-8" ;   
  156.     HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;   
  157.     HttpContext.Current.Response.ContentType =  "application/ms-excel" ; //image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword    
  158.     ctl.Page.EnableViewState =  false ;   
  159.     System.IO.StringWriter tw =  new  System.IO.StringWriter();   
  160.     System.Web.UI.HtmlTextWriter hw =  new  System.Web.UI.HtmlTextWriter(tw);   
  161.     ctl.RenderControl(hw);   
  162.     HttpContext.Current.Response.Write(tw.ToString());   
  163.     HttpContext.Current.Response.End();   
  164. }    
  165.   
  166. ///using System.Security.Cryptography;   
  167. ///using System.Text;   
  168. /// <summary>   
  169. /// MD5函数   
  170. /// </summary>   
  171. /// <param name="str">原始字符串</param>   
  172. /// <returns>MD5结果</returns>   
  173. public   static   string  MD5( string  str)   
  174. {   
  175.      byte [] b = Encoding.Default.GetBytes(str);   
  176.     b =  new  MD5CryptoServiceProvider().ComputeHash(b);   
  177.      string  ret =  "" ;   
  178.      for  ( int  i = 0; i < b.Length; i++)   
  179.         ret += b[i].ToString( "x" ).PadLeft(2,  '0' );   
  180.      return  ret;   
  181. }   
  182.   
  183.   
  184. ///using System.Net;       
  185. ///using System.IO;     
  186. /// <summary>       
  187. /// 根据Url获得源文件内容       
  188. /// </summary>       
  189. /// <param name="url">合法的Url地址</param>       
  190. /// <returns></returns>       
  191. public   static   string  GetSourceTextByUrl( string  url)       
  192. {       
  193.     WebRequest request = WebRequest.Create(url);       
  194.     request.Timeout = 20000; //20秒超时       
  195.     WebResponse response = request.GetResponse();       
  196.       
  197.     Stream resStream = response.GetResponseStream();       
  198.     StreamReader sr =  new  StreamReader(resStream);       
  199.      return  sr.ReadToEnd();       
  200. }    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值