.net Jscript 公共类
public static class Jscript { /// <summary> /// 弹出提示框 /// </summary> public static void Alert(Page page, string msg) { string js = @"alert('" + msg + "');"; if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "Alert")) page.ClientScript.RegisterStartupScript(page.GetType(), "Alert", js, true); } /// <summary> /// 弹出提示框(防止局部刷新没有弹出提示框) /// </summary> public static void Alert(Control control, string msg) { string js = @"alert('" + msg + "');"; ScriptManager.RegisterStartupScript(control, control.GetType(), "Alert", js, true); } /// <summary> /// 控件点击 消息确认提示框 /// </summary> /// <param name="page">当前页面指针,一般为this</param> /// <param name="msg">提示信息</param> public static void ShowConfirm(System.Web.UI.WebControls.WebControl Control, string msg) { Control.Attributes.Add("onclick", "return confirm('" + msg + "');"); } /// <summary> /// 弹出提示框并跳转到指定页面 /// </summary> public static void AlertAndRedirect(Page page, string msg, string ToURL) { string js = @"alert('{0}');window.location.replace('{1}');"; js = string.Format(js, msg, ToURL); if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "AlertAndRedirect")) page.ClientScript.RegisterStartupScript(page.GetType(), "AlertAndRedirect", js, true); } /// <summary> /// 返回历史 /// </summary> public static void GoHistory(Page page, int value) { string js = @"history.go('{0}');"; if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "GoHistory")) page.ClientScript.RegisterStartupScript(page.GetType(), "GoHistory", string.Format(js, value), true); } /// <summary> /// 关闭并使父窗口根据URL刷新 /// </summary> public static void RefreshParent(Page page, string url) { string js = @"window.openner.location.href='{0}'; window.close();"; if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "RefreshParent")) page.ClientScript.RegisterStartupScript(page.GetType(), "RefreshParent", string.Format(js, url), true); } //// <summary> /// 打开指定大小的新窗体 /// </summary> /// <param name="url">地址</param> /// <param name="width">宽</param> /// <param name="heigth">高</param> /// <param name="top">头位置</param> /// <param name="left">左位置</param> /// <param name="scoll">是否带滚动条</param> public static void OpenWebFowebnewize(string url, int width, int heigth, int top, int left, bool scoll, Page page) { string js = @"window.open('" + url + @"','','height=" + heigth + ",width=" + width + ",top=" + top + ",left=" + left + "," + "location=no,menubar=no,resizable=no,scrollbars=" + (scoll ? "yes" : "no") + ",status=yes,titlebar=no,toolbar=no,directories=no');"; if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "OpenWebFowebnewize")) { page.ClientScript.RegisterStartupScript(page.GetType(), "OpenWebFowebnewize", js, true); //HttpContext.Current.Server.Transfer } } public static void OpenModalDialog(string ModalDialogURL, string parentwin, Page page) { string js = @"window.showModalDialog('{0}','{1}');"; js = string.Format(js, ModalDialogURL, parentwin); //if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "OpenModalDialog")) // page.ClientScript.RegisterStartupScript(page.GetType(), "OpenModalDialog", js, true); HttpContext.Current.Response.Write("<script>" + js + "</script>"); } /// <summary> /// 转向指定路径 /// </summary> public static void TransferURL(Page page, string URL) { string js = @"window.location.href='{0}'"; js = string.Format(js, URL); if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "TransferURL")) { page.ClientScript.RegisterStartupScript(page.GetType(), "TransferURL", js, true); } } /// <summary> /// 执行JS /// </summary> public static void WriteJs(Page page, string js) { page.ClientScript.RegisterStartupScript(page.GetType(), "WriteJs", js, true); } /// <summary> /// 关闭窗口 /// </summary> public static void CloseWindow(Page page) { string js = @"window.close()"; if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "CloseWindow")) { page.ClientScript.RegisterStartupScript(page.GetType(), "CloseWindow", js, true); } } }
作者:唐小熊
出处:http://www.cnblogs.com/IT-Bear/
关于作者:一头写代码的熊
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接
如有问题,可以通过kumat@foxmail.com 联系我,非常感谢。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构