.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);
            }
        }
    }
复制代码
posted @   kumat  阅读(347)  评论(0编辑  收藏  举报
编辑推荐:
· .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语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示