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

这篇文章主要介绍了ASP.NET中常用输出JS脚本的类实例,针对过去的js脚本输出类进行了一定的改进,在进行asp.net程序开发时非常具有实用价值,需要的朋友可以参考下

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

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

复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;

///


/// 一些常用的Js调用
/// 添加新版说明:由于旧版普遍采用Response.Write(string msg)的方式输出js脚本,这种
/// 方式输出的js脚本会在html元素的标签之外,破坏了整个xhtml的结构,
/// 而新版本则采用ClientScript.RegisterStartupScript(string msg)的方式输出,不会改变xhtml的结构,
/// 不会影响执行效果。
/// 为了向下兼容,所以新版本采用了重载的方式,新版本中要求一个System.Web.UI.Page类的实例。
/// 创建者:马先光
/// 新版作者:周公
/// 修改版发布网址:http://blog.csdn.net/zhoufoxcn
///

public class JScript
{
    #region 旧版本
    ///
    /// 弹出JavaScript小窗口
    ///

    /// 窗口信息
    public static void Alert(string message)
    {
        #region
        string js = @"";
        HttpContext.Current.Response.Write(js);
        #endregion
    }

    ///


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

    /// 消息内容
    /// 连接地址
    public static void AlertAndRedirect(string message, string toURL)
    {
        #region
        string js = "";
        HttpContext.Current.Response.Write(string.Format(js, message, toURL));
        #endregion
    }

    ///


    /// 回到历史页面
    ///

    /// -1/1
    public static void GoHistory(int value)
    {
        #region
        string js = @"";
        HttpContext.Current.Response.Write(string.Format(js, value));
        #endregion
    }

    ///


    /// 关闭当前窗口
    ///

    public static void CloseWindow()
    {
        #region
        string js = @"";
        HttpContext.Current.Response.Write(js);
        HttpContext.Current.Response.End();
        #endregion
    }

    ///


    /// 刷新父窗口
    ///

    public static void RefreshParent(string url)
    {
        #region
        string js = @"";
        HttpContext.Current.Response.Write(js);
        #endregion
    }


    ///


    /// 刷新打开窗口
    ///

    public static void RefreshOpener()
    {
        #region
        string js = @"";
        HttpContext.Current.Response.Write(js);
        #endregion
    }


    ///


    /// 打开指定大小的新窗体
    ///

    /// 地址
    ///
    ///
    /// 头位置
    /// 左位置
    public static void OpenWebFormSize(string url, int width, int heigth, int top, int left)
    {
        #region
        string js = @"";

        HttpContext.Current.Response.Write(js);
        #endregion
    }


    ///


    /// 转向Url制定的页面
    ///

    /// 连接地址
    public static void JavaScriptLocationHref(string url)
    {
        #region
        string js = @"";
        js = string.Format(js, url);
        HttpContext.Current.Response.Write(js);
        #endregion
    }

    ///


    /// 打开指定大小位置的模式对话框
    ///

    /// 连接地址
    ///
    ///
    /// 距离上位置
    /// 距离左位置
    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
    }
    ///
    /// 弹出模态窗口
    ///

    ///
    ///
    public static void ShowModalDialogWindow(string webFormUrl, string features)
    {
        string js = ShowModalDialogJavascript(webFormUrl, features);
        HttpContext.Current.Response.Write(js);
    }
    ///
    /// 弹出模态窗口
    ///

    ///
    ///
    ///
    public static string ShowModalDialogJavascript(string webFormUrl, string features)
    {
        #region
        string js = @"";
        return js;
        #endregion
    }
    #endregion

    #region 新版本
    ///


    /// 弹出JavaScript小窗口
    ///

    /// 窗口信息
    public static void Alert(string message, Page page)
    {
        #region
        string js = @"";
        //HttpContext.Current.Response.Write(js);
        if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "alert"))
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "alert", js);
        }
        #endregion
    }

    ///


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

    /// 消息内容
    /// 连接地址
    public static void AlertAndRedirect(string message, string toURL, Page page)
    {
        #region
        string js = "";
        //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
    }

    ///


    /// 回到历史页面
    ///

    /// -1/1
    public static void GoHistory(int value, Page page)
    {
        #region
        string js = @"";
        //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
    }

    //        ///


    //        /// 关闭当前窗口
    //        ///

    //        public static void CloseWindow()
    //        {
    //            #region
    //            string js = @"";
    //            HttpContext.Current.Response.Write(js);
    //            HttpContext.Current.Response.End();
    //            #endregion
    //        }

    ///


    /// 刷新父窗口
    ///

    public static void RefreshParent(string url, Page page)
    {
        #region
        string js = @"";
        //HttpContext.Current.Response.Write(js);
        if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "RefreshParent"))
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "RefreshParent", js);
        }
        #endregion
    }


    ///


    /// 刷新打开窗口
    ///

    public static void RefreshOpener(Page page)
    {
        #region
        string js = @"";
        //HttpContext.Current.Response.Write(js);
        if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "RefreshOpener"))
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "RefreshOpener", js);
        }
        #endregion
    }


    ///


    /// 打开指定大小的新窗体
    ///

    /// 地址
    ///
    ///
    /// 头位置
    /// 左位置
    public static void OpenWebFormSize(string url, int width, int heigth, int top, int left, Page page)
    {
        #region
        string js = @"";
        //HttpContext.Current.Response.Write(js);
        if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "OpenWebFormSize"))
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "OpenWebFormSize", js);
        }
        #endregion
    }


    ///


    /// 转向Url制定的页面
    ///

    /// 连接地址
    public static void JavaScriptLocationHref(string url, Page page)
    {
        #region
        string js = @"";
        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
    }

    ///


    /// 打开指定大小位置的模式对话框
    ///

    /// 连接地址
    ///
    ///
    /// 距离上位置
    /// 距离左位置
    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
    }
    ///
    /// 弹出模态窗口
    ///

    ///
    ///
    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);
        }
    }
    //        ///
    //        /// 弹出模态窗口
    //        ///

    //        ///
    //        ///
    //        ///
    //        public static string ShowModalDialogJavascript(string webFormUrl, string features)
    //        {
    //            #region
    //            string js = @"";
    //            return js;
    //            #endregion
    //        }
    #endregion
}

以上就是ASP.NET中常用输出JS脚本的类实例的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程