asp.net常用函数收藏

 更新时间:2020年06月16日 22:27:47   投稿:mdxy-dxy  
这篇文章给大家介绍asp.net常用函数收藏,主要包括 得到站点用户IP,去除字符串最后一个','号、去除字符串第一个'/'号等,感兴趣的朋友跟随小编一起看看吧
//弹出对话框 public static void salert(string str) { HttpContext.Current.Response.Write("<script>alert('" + str + "');</script>"); } /// <summary> /// 显示消息提示框,并回到前一页面 /// </summary> /// <param name="page">当前页面指针,一般为this</param> /// <param name="strMsg">提示信息</param> public static void ShowGoHistory(System.Web.UI.Page page, string strMsg) { page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + strMsg.ToString() + "');window.history.go(-1);</script>"); } /// <summary> /// 显示消息提示对话框,并进行页面跳转 /// </summary> /// <param name="page">当前页面指针,一般为this</param> /// <param name="strMsg">提示信息</param> /// <param name="url"> 跳转的目标URL</param> public static void ShowRedirect(System.Web.UI.Page page, string strMsg, string url) { StringBuilder Builder = new StringBuilder(); Builder.Append("<script language='javascript' defer>"); Builder.AppendFormat("alert('{0}');", strMsg); Builder.AppendFormat("top.location.href='{0}'", url); Builder.Append("</script>"); page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString()); } //为了插入单引号 public static string delSingle(string str) { return str.Replace("'", "''"); } //由gridviw导出为Excel public static void ToExcel(System.Web.UI.Control ctl) { HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls"); HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword ctl.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); } ///using System.Security.Cryptography; ///using System.Text; /// <summary> /// MD5函数 /// </summary> /// <param name="str">原始字符串</param> /// <returns>MD5结果</returns> public static string MD5(string str) { byte[] b = Encoding.Default.GetBytes(str); b = new MD5CryptoServiceProvider().ComputeHash(b); string ret = ""; for (int i = 0; i < b.Length; i++) ret += b[i].ToString("x").PadLeft(2, '0'); return ret; } ///using System.Net; ///using System.IO; /// <summary> /// 根据Url获得源文件内容 /// </summary> /// <param name="url">合法的Url地址</param> /// <returns></returns> public static string GetSourceTextByUrl(string url) { WebRequest request = WebRequest.Create(url); request.Timeout = 20000;//20秒超时 WebResponse response = request.GetResponse(); Stream resStream = response.GetResponseStream(); StreamReader sr = new StreamReader(resStream); return sr.ReadToEnd(); }

总结

到此这篇关于asp.net常用函数收藏的文章就介绍到这了,更多相关asp.net常用函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论