程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> ASP.NET中常用輸出JS腳本的類實例

ASP.NET中常用輸出JS腳本的類實例

編輯:ASP.NET基礎

本文實例講述了ASP.NET中常用輸出JS腳本的類,針對過去輸出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#程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved