[置顶] 当前上下文不存在 ScriptManager 的原因分析以及解决方案 (System.Web.Extensions)
错误如下图:
当我们在写web程序的时候,有时候避免不了要再后台弹出一个提示框,在winform中我们可以用MssageBox() 类来实现,在web开发中,我们只能借助于javascript来实现了
.net中为我们提供了这样一个类ScriptManager来帮助我们解决这样的问题,而有些时候我们却在使用的时候容易出现这样一个错误,”当前上下文不存在 ScriptManager ” ,这就很郁闷了,差资料显示 ScriptManager 类存在于 System.Web.UI 命名空间中,这时我们在类的头部添加引用,using System.Web.UI;或者是继承 System.Web.UI.Page 这样还是不行,郁闷了,查资料,显示 System.Web.UI类存在于 程序集: System.Web.Extensions(在 System.Web.Extensions.dll 中),这时,有人会在类头部这样写,Using System.Web.Extensions; 这样是不行的,我们必须在项目中,添加引用,
示例如下图:
第二步:
“
msdn论坛:http://msdn.microsoft.com/zh-cn/library/system.web.ui.scriptmanager%28v=vs.100%29.aspx点击打开链接
示例类如下:类PageUtil
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; namespace shouqiang_Wei { public class PageUtil:System.Web.UI.Page { #region 截取字符长度 /// <summary> /// 截取字符长度 /// </summary> /// <param name="inputString">字符</param> /// <param name="len">长度</param> /// <returns></returns> public static string CutString(string inputString, int len) { if (string.IsNullOrEmpty(inputString)) return ""; inputString = DropHTML(inputString); ASCIIEncoding ascii = new ASCIIEncoding(); int tempLen = 0; string tempString = ""; byte[] s = ascii.GetBytes(inputString); for (int i = 0; i < s.Length; i++) { if ((int)s[i] == 63) { tempLen += 2; } else { tempLen += 1; } try { tempString += inputString.Substring(i, 1); } catch { break; } if (tempLen > len) break; } //如果截过则加上半个省略号 byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString); if (mybyte.Length > len) tempString += "…"; return tempString; } #endregion #region 清除HTML标记 public static string DropHTML(string Htmlstring) { if (string.IsNullOrEmpty(Htmlstring)) return ""; //删除脚本 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 /// <summary> /// 在web页面上显示一个对话框 /// </summary> /// <param name="page"></param> /// <param name="msg"></param> public static void ShowMsgBox(System.Web.UI.Control page, string msg) { string m = HttpUtility.HtmlEncode(msg); ExecJavaScript(page, "alert('" + m + "');"); } public static void ShowCfmMsgBox(System.Web.UI.Control page, string msg) { string m = HttpUtility.HtmlEncode(msg); ExecJavaScript(page, "confirm('" + m + "');"); } /// <summary> /// 显示提示信息并转向新的页面 /// </summary> /// <param name="page"></param> /// <param name="msg"></param> /// <param name="newurl"></param> public static void ShowMsgBoxAndRedirect(System.Web.UI.Control page, string msg, string newurl) { string m = HttpUtility.HtmlEncode(msg); ExecJavaScript(page, "alert('" + m + "'); window.location='" + newurl + "';"); } /// <summary> /// Add by shouqiang Wei 改成 confirm 这样的话就可以返回了...> 不知道能不能行? /// </summary> /// <param name="page"></param> /// <param name="msg"></param> /// <param name="newurl"></param> public static void ShowCfmMsgBoxAndRedirect(System.Web.UI.Control page, string msg, string newurl) { string m = HttpUtility.HtmlEncode(msg); ExecJavaScript(page, "if(confirm('" + m + "')) window.location='" + newurl + "' ;"); } /// <summary> /// 子框架中转向 /// </summary> /// <param name="page"></param> /// <param name="newurl"></param> public static void ParentPageRedirect(System.Web.UI.Control page, string newurl) { ExecJavaScript(page, " self.parent.window.location='" + newurl + "';"); } /// <summary> /// 在web页面上添加一段javascript /// </summary> /// <param name="page"></param> /// <param name="script"></param> public static void ExecJavaScript(System.Web.UI.Control page, string script) { ScriptManager.RegisterStartupScript(page, page.GetType(), "a", script, true); } /// <summary> /// MD5摘要 /// </summary> /// <param name="cleanString"></param> /// <returns></returns> public static string Md5Encrypt(string cleanString) { Byte[] clearBytes = new UnicodeEncoding().GetBytes(cleanString); Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes); return BitConverter.ToString(hashedBytes); } /// <summary> /// SQL防注入攻击,数据过滤 /// </summary> /// <param name="value">要过滤的数据</param> /// <returns>过滤后的数据</returns> public static string SQLFilter(string value) { if (String.IsNullOrEmpty(value)) return ""; value = value.Trim(); value = HttpUtility.HtmlEncode(value); value = value.Replace(" and ", "***"); value = value.Replace(" or ", "***"); value = value.Replace(" truncate ", "***"); value = value.Replace(" from ", "***"); value = value.Replace(" select ", "***"); value = value.Replace(" insert ", "***"); value = value.Replace(" update ", "***"); value = value.Replace(" delete ", "***"); value = value.Replace(" drop ", "***"); value = value.Replace(" count ", "***"); value = value.Replace(" exec ", "***"); return value; } public static string GetSex(object obj) { if (obj == null) { return "未知"; } try { bool sex = bool.Parse(obj.ToString()); if (sex) { return "男"; } else return "女"; } catch (Exception ex) { return "未知"; } } public static string GetClosedStatus(object obj) { if (obj == null) { return "未知"; } else { try { int n = int.Parse(obj.ToString()); if (n == 1) { return "关闭"; } else if (n == 0) return "开放"; else return "未知"; } catch (Exception ex) { return "未知"; } } } public static string SubString(object obj, int length) { if (obj == null) return ""; string str = obj.ToString(); if (str.Length > length) return str.Substring(0, length); return str; } } }