后台注册JS脚本 弹窗 跳转 报错 等

本文介绍了一种在ASP.NET中实现页面跳转并伴随提示信息的方法。提供了三种不同场景下的实用函数:TransferTo用于普通页面跳转,RegistJSRefushOpenForm用于关闭窗口并刷新父页面,而TransferToParent则专门用于关闭弹窗并给出提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

          /// <summary>
         /// 页面跳转,含提示
        /// </summary>
        /// <param name="strMsg"></param>
        /// <param name="strUrl"></param>
        public void TransferTo(string strUrl, string strMsg)
        {
            if (string.IsNullOrEmpty(strMsg))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", string.Format("<script>window.location.href='{0}'</script>", strUrl));
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", string.Format("<script>alert('{0}');window.location.href='{1}'</script>", strMsg, strUrl));
            }
        }

        /// <summary>
        /// 注册弹出消息并关闭当前窗口及刷新后台页面
        /// </summary>
        /// <param name="mgs"></param>
        public void RegistJSRefushOpenForm(string mgs)
        {
            string message = string.Format("alert(\"{0}\");window.opener.location.href=window.opener.location.href; self.close()", mgs);
            string script = " <script language=\"javascript\" type=\"text/javascript\"> " + message + "</script>";


            Page.ClientScript.RegisterStartupScript(Page.GetType(), "ok", script);
        }

        /// <summary>
        /// 提示错误后,关闭弹出窗
        /// </summary>
        /// <param name="strMsg"></param>
        public void TransferToParent(string strMsg)
        {
            if (string.IsNullOrEmpty(strMsg))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "closePop", string.Format("<script>window.frameElement.cancelPopUp();</script>"));
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "closePop", string.Format("<script>alert('{0}');window.frameElement.cancelPopUp();</script>", strMsg));
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值