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

我们在编写ASP.NET程序时,经常会在服务器端调用JS,提示信息,例如弹出提示框、弹出确认框、页面跳转等等。下面为大家列出了通用的调用方法,大家可以将这些方法放在共用类里,其它的程序共享调用就可以了。

     1、弹出JavaScript小窗口

    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));    }

        2、弹出消息框并且转向到新的URL

    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));    }

        3、出消息框并且转向到指定FRAME的新URL        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));    }

        4、服务器端弹出confirm对话框,并根据选择触发某按钮点击事重定向URL。        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>");    }

        5、服务器端弹出confirm对话框,并根据选择触发某按钮点击事件。        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>");    }

       6、服务器端弹出confirm对话框,并根据选择触发某按钮点击事重定向URL,包括“确定”和“取消”时的操作        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>");    }

        7、回到历史页面        public static void GoHistory(int value)    {        string js = "<script language='JavaScript'>history.go({0});</script>";        HttpContext.Current.Response.Write(string.Format(js, value));    }

      8、关闭当前窗口       public static void CloseWindow()    {        string js = "<script language='JavaScript'>parent.opener=null;window.close();</script>";        HttpContext.Current.Response.Write(js);        HttpContext.Current.Response.End();    }

      9、父窗口打开指定页面        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));    }

        10、刷新窗口       public static void RefreshWindow()    {        string js = "<script language='JavaScript'>opener.location.reload();</script>";        HttpContext.Current.Response.Write(js);    }

       11、 打开指定大小的新窗体        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));    }

       12、打开新窗体        public static void OpenWindow(string url)    {        string js = "<script language='JavaScript'>window.open('{0}');</script>";        HttpContext.Current.Response.Write(string.Format(js, url));    }

    

   13、 转向指定窗口指定Url的页面        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));    }

   

   14、转向指定Url的页面        public static void JavaScriptLocationHref(string url)    {        JsGoTo("window", url);    }

      15、转向top指定Url的页面        public static void TopGoTo(string url)    {        JsGoTo("top", url);    }

       16、转向parent指定Url的页面        public static void ParentGoTo(string url)    {        JsGoTo("parent", url);    }

      17、打开指定大小位置的模式对话框        public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left)    {        string features = "dialogWidth:" + width.ToString() + "px"            + ";dialogHeight:" + height.ToString() + "px"            + ";dialogLeft:" + left.ToString() + "px"            + ";dialogTop:" + top.ToString() + "px"            + ";center:yes;help=no;resizable:no;status:no;scroll=yes";        ShowModalDialogWindow(webFormUrl, features);    }

        18、打开模式窗口        public static void ShowModalDialogWindow(string webFormUrl, string features)    {        string js = ShowModalDialogJavascript(webFormUrl, features);        HttpContext.Current.Response.Write(js);    }

   

   19、返回模式窗口脚本       public static string ShowModalDialogJavascript(string webFormUrl, string features)    {        string js = "<script language=javascript>showModalDialog('" + webFormUrl + "','','" + features + "');</script>";        return js;    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值