using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; public static class JS { public static Page CurrPage { get { return HttpContext.Current.Handler as Page; } } public static void AddConfirmToControl(IAttributeAccessor control, string message) { message = message.Replace("\\", "\\\\").Replace("\n", "\\n").Replace("\'", "\\'"); control.SetAttribute("onclick", "return confirm('" + message + "');"); } /// <summary> /// 直接关闭页面(忽略其他提示) /// </summary> public static void ClosePage() { HttpContext.Current.Response.Write(@"<script>window.opener=null;window.close();</script>"); HttpContext.Current.Response.End(); } /// <summary> /// 给控件增加客户端属性 /// </summary> /// <param name="control">控件</param> /// <param name="attributeName">属性名</param> /// <param name="attributeValue">属性值</param> /// <example> /// JS.AddAttributesToControl(TextBox1,"oncopy","return false;"); --生成的客户端html中增加 oncopy="return false;" /// </example> public static void AddAttributesToControl(IAttributeAccessor control, string attributeName, string attributeValue) { control.SetAttribute(attributeName, attributeValue); } /// <summary> /// 添加要执行的脚本 /// </summary> /// <param name="scripts">脚本内容</param> /// <param name="key">脚本的键(如果该键已有则不重复创建)</param> public static void AddScript(string scripts, string key) { StringBuilder sb = new StringBuilder("<script language='javascript'>"); sb.Append(scripts); sb.Replace("\\", "\\\\"); sb.Replace("\n", "\\n"); sb.Replace("\t", "\\t"); sb.Replace("\r", "\\r"); sb.Append("</script>"); CurrPage.ClientScript.RegisterStartupScript(CurrPage.GetType(), key, sb.ToString()); } /// <summary> /// 打开窗口(不允许一个页面打开多个窗口) /// </summary> /// <param name="page">this</param> /// <param name="URL">地址</param> /// <param name="Target">目标框架</param> public static void open(string url, string target) { AddScript("window.open('" + url + "','" + target + "');", "redirect"); } /// <summary> /// 打开一个窗口(弹出窗口的菜单栏、工具栏和地址栏等被屏蔽) /// </summary> /// <param name="url">目标窗口URL</param> /// <param name="width">窗口宽度</param> /// <param name="height">窗口高度</param> public static void open(string url, int width, int height) { string s = string.Format("<script language='javascript'>window.open(\"{0}\",\"_blank\",\"height={1},width={2},top=100,left=100,toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=yes\");</script>", url, height, width); CurrPage.ClientScript.RegisterStartupScript(CurrPage.GetType(), null, s, false); } /// <summary> /// 跳转页面 /// </summary> /// <param name="url">地址</param> public static void location(string url) { AddScript("window.location.href='" + url + "';", "redirect"); } public static void AddHead(string script) { StringBuilder sb = new StringBuilder("<script language=\"javascript\">"); sb.Append(script); sb.Append("</script>"); CurrPage.Response.Write(sb.ToString()); } public static void alert(string message) { AddScript("alert(\"" + message + "\");", "alert"); } public static void alert(int Icon, string Message) { StringBuilder sb = new StringBuilder(); Message = Message.Replace("\"", "\u201c"); Message = Message.Replace("'", "\u2018"); sb.Append("<script language=vbscript>\n"); switch (Icon) { case 1: sb.Append("msgbox \"" + Message + "\",\"64\"\n"); break; case 2: sb.Append("msgbox \"" + Message + "\",\"16\"\n"); break; case 3: sb.Append("msgbox \"" + Message + "\",\"48\"\n"); break; } sb.Append("</script>"); CurrPage.ClientScript.RegisterStartupScript(typeof(string), "ALERT", sb.ToString()); } public static void AlertAndRedirect(string message, string url, string target) { alert(message); open(url, target); } public static void AlertAndRedirect(string message, string url) { alert(message); location(url); } public static string GetClientIP() { return HttpContext.Current.Request.UserHostAddress; } public static void refreshParent(string strIndex) { HttpContext.Current.Response.Write("<script language=javascript>var str=window.opener.location.href; var ch=str.indexOf(\"?\"); if(ch>=0){var i=str.indexOf(\"?index\");if(i<0){i=str.indexOf(\"&index\")} if (i>=0){i=i+1;var strTmp = str.substr(i);var itemp = strTmp.indexOf(\"&\");if(itemp>0){strTmp=strTmp.substr(itemp);}str=str.replace(strTmp, \"\");}else{str+=\"&\";}}else{str+=\"?\";}str += \"index=" + strIndex + "\"; opener.document.location=str; window.close();</script>"); } }
posted on
2008-05-06 14:28
heart-in-sky
阅读(
259)
评论()
编辑
收藏
举报