欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET中常用输出JS脚本的类实例

程序员文章站 2024-02-24 10:06:55
本文实例讲述了asp.net中常用输出js脚本的类,针对过去进行了一定的改进。在项目开发中非常具有实用价值。分享给大家供大家参考。具体如下: 很多时候在asp.net中我...

本文实例讲述了asp.net中常用输出js脚本的类,针对过去进行了一定的改进。在项目开发中非常具有实用价值。分享给大家供大家参考。具体如下:

很多时候在asp.net中我们经常需要输出一些js脚本,比如弹出一个警告窗口,返回到历史页面等js功能,我看到网上流传得比较广的是马先光写的一个jscript类,这个类基本将经常用到的js脚本包含了,非常方便,唯一的不足是作者采用的response.write(string msg)的办法,这样造成输出的js脚本在<html></html>标签之外,破坏了原有xhtml的结构,所以本人在满足原功能的情况下,对jscript类做了进一步的改善,这个改善采用了重载的办法,增加了一个system.web.ui.page类的实例作为参数,不会影响原来的程序代码。
整个程序的代码如下:

复制代码 代码如下:
using system;
using system.collections.generic;
using system.text;
using system.web;
using system.web.ui;

/// <summary>
/// 一些常用的js调用
/// 添加新版说明:由于旧版普遍采用response.write(string msg)的方式输出js脚本,这种
/// 方式输出的js脚本会在html元素的<html></html>标签之外,破坏了整个xhtml的结构,
/// 而新版本则采用clientscript.registerstartupscript(string msg)的方式输出,不会改变xhtml的结构,
/// 不会影响执行效果。
/// 为了向下兼容,所以新版本采用了重载的方式,新版本中要求一个system.web.ui.page类的实例。
/// 创建者:马先光
/// 新版作者:周公
/// 修改版发布网址:http://blog.csdn.net/zhoufoxcn
/// </summary>
public class jscript
{
    #region 旧版本
    /// <summary>
    /// 弹出javascript小窗口
    /// </summary>
    /// <param name="js">窗口信息</param>
    public static void alert(string message)
    {
        #region
        string js = @"<script language='javascript'>
                    alert('" + message + "');</script>";
        httpcontext.current.response.write(js);
        #endregion
    }

    /// <summary>
    /// 弹出消息框并且转向到新的url
    /// </summary>
    /// <param name="message">消息内容</param>
    /// <param name="tourl">连接地址</param>
    public static void alertandredirect(string message, string tourl)
    {
        #region
        string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
        httpcontext.current.response.write(string.format(js, message, tourl));
        #endregion
    }

    /// <summary>
    /// 回到历史页面
    /// </summary>
    /// <param name="value">-1/1</param>
    public static void gohistory(int value)
    {
        #region
        string js = @"<script language='javascript'>
                    history.go({0}); 
                  </script>";
        httpcontext.current.response.write(string.format(js, value));
        #endregion
    }

    /// <summary>
    /// 关闭当前窗口
    /// </summary>
    public static void closewindow()
    {
        #region
        string js = @"<script language='javascript'>
                    parent.opener=null;window.close(); 
                  </script>";
        httpcontext.current.response.write(js);
        httpcontext.current.response.end();
        #endregion
    }

    /// <summary>
    /// 刷新父窗口
    /// </summary>
    public static void refreshparent(string url)
    {
        #region
        string js = @"<script language='javascript'>
                    window.opener.location.href='" + url + "';window.close();</script>";
        httpcontext.current.response.write(js);
        #endregion
    }


    /// <summary>
    /// 刷新打开窗口
    /// </summary>
    public static void refreshopener()
    {
        #region
        string js = @"<script language='javascript'>
                    opener.location.reload();
                  </script>";
        httpcontext.current.response.write(js);
        #endregion
    }


    /// <summary>
    /// 打开指定大小的新窗体
    /// </summary>
    /// <param name="url">地址</param>
    /// <param name="width">宽</param>
    /// <param name="heigth">高</param>
    /// <param name="top">头位置</param>
    /// <param name="left">左位置</param>
    public static void openwebformsize(string url, int width, int heigth, int top, int left)
    {
        #region
        string js = @"<script language='javascript'>window.open('" + url + @"','','height=" + heigth + ",width=" + width + ",top=" + top + ",left=" + left + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');</script>";

        httpcontext.current.response.write(js);
        #endregion
    }


    /// <summary>
    /// 转向url制定的页面
    /// </summary>
    /// <param name="url">连接地址</param>
    public static void javascriptlocationhref(string url)
    {
        #region
        string js = @"<script language='javascript'>
                    window.location.replace('{0}');
                  </script>";
        js = string.format(js, url);
        httpcontext.current.response.write(js);
        #endregion
    }

    /// <summary>
    /// 打开指定大小位置的模式对话框
    /// </summary>
    /// <param name="webformurl">连接地址</param>
    /// <param name="width">宽</param>
    /// <param name="height">高</param>
    /// <param name="top">距离上位置</param>
    /// <param name="left">距离左位置</param>
    public static void showmodaldialogwindow(string webformurl, int width, int height, int top, int left)
    {
        #region
        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);
        #endregion
    }
    /// <summary>
    /// 弹出模态窗口
    /// </summary>
    /// <param name="webformurl"></param>
    /// <param name="features"></param>
    public static void showmodaldialogwindow(string webformurl, string features)
    {
        string js = showmodaldialogjavascript(webformurl, features);
        httpcontext.current.response.write(js);
    }
    /// <summary>
    /// 弹出模态窗口
    /// </summary>
    /// <param name="webformurl"></param>
    /// <param name="features"></param>
    /// <returns></returns>
    public static string showmodaldialogjavascript(string webformurl, string features)
    {
        #region
        string js = @"<script language=javascript>                           
                            showmodaldialog('" + webformurl + "','','" + features + "');</script>";
        return js;
        #endregion
    }
    #endregion

    #region 新版本
    /// <summary>
    /// 弹出javascript小窗口
    /// </summary>
    /// <param name="js">窗口信息</param>
    public static void alert(string message, page page)
    {
        #region
        string js = @"<script language='javascript'>
                    alert('" + message + "');</script>";
        //httpcontext.current.response.write(js);
        if (!page.clientscript.isstartupscriptregistered(page.gettype(), "alert"))
        {
            page.clientscript.registerstartupscript(page.gettype(), "alert", js);
        }
        #endregion
    }

    /// <summary>
    /// 弹出消息框并且转向到新的url
    /// </summary>
    /// <param name="message">消息内容</param>
    /// <param name="tourl">连接地址</param>
    public static void alertandredirect(string message, string tourl, page page)
    {
        #region
        string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
        //httpcontext.current.response.write(string.format(js, message, tourl));
        if (!page.clientscript.isstartupscriptregistered(page.gettype(), "alertandredirect"))
        {
            page.clientscript.registerstartupscript(page.gettype(), "alertandredirect", string.format(js, message, tourl));
        }
        #endregion
    }

    /// <summary>
    /// 回到历史页面
    /// </summary>
    /// <param name="value">-1/1</param>
    public static void gohistory(int value, page page)
    {
        #region
        string js = @"<script language='javascript'>
                    history.go({0}); 
                  </script>";
        //httpcontext.current.response.write(string.format(js, value));
        if (!page.clientscript.isstartupscriptregistered(page.gettype(), "gohistory"))
        {
            page.clientscript.registerstartupscript(page.gettype(), "gohistory", string.format(js, value));
        }
        #endregion
    }

    //        /// <summary>
    //        /// 关闭当前窗口
    //        /// </summary>
    //        public static void closewindow()
    //        {
    //            #region
    //            string js = @"<script language='javascript'>
    //                    parent.opener=null;window.close(); 
    //                  </script>";
    //            httpcontext.current.response.write(js);
    //            httpcontext.current.response.end();
    //            #endregion
    //        }

    /// <summary>
    /// 刷新父窗口
    /// </summary>
    public static void refreshparent(string url, page page)
    {
        #region
        string js = @"<script language='javascript'>
                    window.opener.location.href='" + url + "';window.close();</script>";
        //httpcontext.current.response.write(js);
        if (!page.clientscript.isstartupscriptregistered(page.gettype(), "refreshparent"))
        {
            page.clientscript.registerstartupscript(page.gettype(), "refreshparent", js);
        }
        #endregion
    }


    /// <summary>
    /// 刷新打开窗口
    /// </summary>
    public static void refreshopener(page page)
    {
        #region
        string js = @"<script language='javascript'>
                    opener.location.reload();
                  </script>";
        //httpcontext.current.response.write(js);
        if (!page.clientscript.isstartupscriptregistered(page.gettype(), "refreshopener"))
        {
            page.clientscript.registerstartupscript(page.gettype(), "refreshopener", js);
        }
        #endregion
    }


    /// <summary>
    /// 打开指定大小的新窗体
    /// </summary>
    /// <param name="url">地址</param>
    /// <param name="width">宽</param>
    /// <param name="heigth">高</param>
    /// <param name="top">头位置</param>
    /// <param name="left">左位置</param>
    public static void openwebformsize(string url, int width, int heigth, int top, int left, page page)
    {
        #region
        string js = @"<script language='javascript'>window.open('" + url + @"','','height=" + heigth + ",width=" + width + ",top=" + top + ",left=" + left + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');</script>";
        //httpcontext.current.response.write(js);
        if (!page.clientscript.isstartupscriptregistered(page.gettype(), "openwebformsize"))
        {
            page.clientscript.registerstartupscript(page.gettype(), "openwebformsize", js);
        }
        #endregion
    }


    /// <summary>
    /// 转向url制定的页面
    /// </summary>
    /// <param name="url">连接地址</param>
    public static void javascriptlocationhref(string url, page page)
    {
        #region
        string js = @"<script language='javascript'>
                    window.location.replace('{0}');
                  </script>";
        js = string.format(js, url);
        //httpcontext.current.response.write(js);
        if (!page.clientscript.isstartupscriptregistered(page.gettype(), "javascriptlocationhref"))
        {
            page.clientscript.registerstartupscript(page.gettype(), "javascriptlocationhref", js);
        }
        #endregion
    }

    /// <summary>
    /// 打开指定大小位置的模式对话框
    /// </summary>
    /// <param name="webformurl">连接地址</param>
    /// <param name="width">宽</param>
    /// <param name="height">高</param>
    /// <param name="top">距离上位置</param>
    /// <param name="left">距离左位置</param>
    public static void showmodaldialogwindow(string webformurl, int width, int height, int top, int left, page page)
    {
        #region
        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, page);
        #endregion
    }
    /// <summary>
    /// 弹出模态窗口
    /// </summary>
    /// <param name="webformurl"></param>
    /// <param name="features"></param>
    public static void showmodaldialogwindow(string webformurl, string features, page page)
    {
        string js = showmodaldialogjavascript(webformurl, features);
        //httpcontext.current.response.write(js);
        if (!page.clientscript.isstartupscriptregistered(page.gettype(), "showmodaldialogwindow"))
        {
            page.clientscript.registerstartupscript(page.gettype(), "showmodaldialogwindow", js);
        }
    }
    //        /// <summary>
    //        /// 弹出模态窗口
    //        /// </summary>
    //        /// <param name="webformurl"></param>
    //        /// <param name="features"></param>
    //        /// <returns></returns>
    //        public static string showmodaldialogjavascript(string webformurl, string features)
    //        {
    //            #region
    //            string js = @"<script language=javascript>                           
    //    showmodaldialog('" + webformurl + "','','" + features + "');</script>";
    //            return js;
    //            #endregion
    //        }
    #endregion
}

希望本文所述对大家的c#程序设计有所帮助。