当前位置:Web前端 > javascript> 正文

ASP.NET服务器端执行脚本的通用方法

时间:2013-7-27类别:Web前端

ASP.NET服务器端执行脚本的通用方法

ASP.NET服务器端执行脚本的通用方法
  •  
  • 下面的方法大家在ASP.NET编程时一般都会用到,提供给大家。
  •  
  •  
  • C# 代码   复制
  • 
     using System.Web;
    
    public class JScript
    {
        /// <summary>
        /// 弹出JavaScript小窗口
        /// </summary>
        public static void Alert(string message)
        {
            string js = "<script language='JavaScript'>alert('{0}');history.go(-1);</script>";
            HttpContext.Current.Response.Write(string.Format(js, message));
        }
    
        /// <summary>
        /// 弹出消息框并且转向到新的URL
        /// </summary>
        public static void Alert(string message, string toURL)
        {
            string js = "<script language='JavaScript'>alert('{0}');window.location.replace('{1}')</script>";
            HttpContext.Current.Response.Write(string.Format(js, message, toURL));
        }
    
        /// <summary>
        /// 弹出消息框并且转向到指定FRAME的新URL
        /// </summary>
        public static void Alert(string message, string frame, string toURL)
        {
            string js = "<script language='JavaScript'>alert('{0}');{1}.location.replace('{2}')</script>";
            HttpContext.Current.Response.Write(string.Format(js, message, frame, toURL));
        }
    
        /// <summary> 
        /// 服务器端弹出confirm对话框,并根据选择触发某按钮点击事重定向URL。
        /// </summary> 
        public static void ConfirmRedirect(Page page, string sMessage, string sUrl)
        {
            page.ClientScript.RegisterStartupScript(typeof(System.String), "", "<script language='javascript' type='text/javascript' defer='defer'> if (confirm('" + sMessage + "')==true){ window.location.href = '" + sUrl + "';}</script>");
        }
    
        /// <summary> 
        /// 服务器端弹出confirm对话框,并根据选择触发某按钮点击事件。
        /// </summary> 
        public static void Confirm(Page page, string sMessage, string btn)
        {
            page.ClientScript.RegisterStartupScript(typeof(System.String), "", "<script language='javascript' type='text/javascript' defer='defer'> if (confirm('" + sMessage + "')==true){document.forms(0)." + btn + ".click();}</script>");
        }
    
        /// <summary> 
        ///  服务器端弹出confirm对话框,并根据选择触发某按钮点击事重定向URL,包括“确定”和“取消”时的操作 
        /// </summary> 
        public static void Confirm(Page page, string sMessage, string btn_Redirect_Yes, string btn_Redirect_No)
        {
            page.ClientScript.RegisterStartupScript(typeof(System.String), "", "<script language='javascript' type='text/javascript' defer='defer'> if (confirm('" + sMessage + "')==true){document.forms(0)." + btn_Redirect_Yes + ".click();}btn_Redirect_Yes{document.forms(0)." + btn_Redirect_No + ".click();}</script>");
        }
    
        /// <summary>
        /// 回到历史页面
        /// </summary>
        public static void GoHistory(int value)
        {
            string js = "<script language='JavaScript'>history.go({0});</script>";
            HttpContext.Current.Response.Write(string.Format(js, value));
        }
    
        /// <summary>
        /// 关闭当前窗口
        /// <summary>
        public static void CloseWindow()
        {
            string js = "<script language='JavaScript'>parent.opener=null;window.close();</script>";
            HttpContext.Current.Response.Write(js);
            HttpContext.Current.Response.End();
        }
    
        /// <summary>
        /// 父窗口打开指定页面
        /// </summary>
        public static void RefreshParent(string url)
        {
            string js = "<script language='JavaScript'>window.opener.location.href='{0}';window.close();</script>";
            HttpContext.Current.Response.Write(string.Format(js, url));
        }
    
        /// <summary>
        /// 刷新窗口
        /// </summary>
        public static void RefreshWindow()
        {
            string js = "<script language='JavaScript'>opener.location.reload();</script>";
            HttpContext.Current.Response.Write(js);
        }
    
        /// <summary>
        /// 打开指定大小的新窗体
        /// </summary>
        public static void OpenWindow(string url, int width, int heigth, int top, int left)
        {
            string js = "<script language='JavaScript'>window.open('{0}', '', 'width={1}, height={2}, top={3}, left={4}, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, titlebar=no, toolbar=no, directories=no');</script>";
            HttpContext.Current.Response.Write(string.Format(js, url, width, heigth, top, left));
        }
    
        /// <summary>
        /// 打开新窗体
        /// </summary>
        public static void OpenWindow(string url)
        {
            string js = "<script language='JavaScript'>window.open('{0}');</script>";
            HttpContext.Current.Response.Write(string.Format(js, url));
        }
    
        /// <summary>
        /// 转向指定窗口指定Url的页面
        /// </summary>
        public static void JsGoTo(string window, string url)
        {
            string js = "<script language='JavaScript'>{0}.location.replace('{1}');</script>";
            HttpContext.Current.Response.Write(string.Format(js, window, url));
        }
    
        /// <summary>
    标签:
  • 上一篇下一篇

    猜您喜欢

    热门推荐