最新文章專題視頻專題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

ASP.NET中常用輸出JS腳本的類實(shí)例

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:38:47
文檔

ASP.NET中常用輸出JS腳本的類實(shí)例

ASP.NET中常用輸出JS腳本的類實(shí)例:本文實(shí)例講述了ASP.NET中常用輸出JS腳本的類,針對(duì)過(guò)去輸出js腳本的類進(jìn)行了一定的改進(jìn)。在項(xiàng)目開(kāi)發(fā)中非常具有實(shí)用價(jià)值。分享給大家供大家參考。具體如下: 很多時(shí)候在ASP.NET中我們經(jīng)常需要輸出一些JS腳本,比如彈出一個(gè)警告窗口,返回到歷史頁(yè)面等JS功能
推薦度:
導(dǎo)讀ASP.NET中常用輸出JS腳本的類實(shí)例:本文實(shí)例講述了ASP.NET中常用輸出JS腳本的類,針對(duì)過(guò)去輸出js腳本的類進(jìn)行了一定的改進(jìn)。在項(xiàng)目開(kāi)發(fā)中非常具有實(shí)用價(jià)值。分享給大家供大家參考。具體如下: 很多時(shí)候在ASP.NET中我們經(jīng)常需要輸出一些JS腳本,比如彈出一個(gè)警告窗口,返回到歷史頁(yè)面等JS功能

本文實(shí)例講述了ASP.NET中常用輸出JS腳本的類,針對(duì)過(guò)去輸出js腳本的類進(jìn)行了一定的改進(jìn)。在項(xiàng)目開(kāi)發(fā)中非常具有實(shí)用價(jià)值。分享給大家供大家參考。具體如下:

很多時(shí)候在ASP.NET中我們經(jīng)常需要輸出一些JS腳本,比如彈出一個(gè)警告窗口,返回到歷史頁(yè)面等JS功能,我看到網(wǎng)上流傳得比較廣的是馬先光寫的一個(gè)JScript類,這個(gè)類基本將經(jīng)常用到的JS腳本包含了,非常方便,唯一的不足是作者采用的Response.Write(string msg)的辦法,這樣造成輸出的js腳本在<html></html>標(biāo)簽之外,破壞了原有XHTML的結(jié)構(gòu),所以本人在滿足原功能的情況下,對(duì)JScript類做了進(jìn)一步的改善,這個(gè)改善采用了重載的辦法,增加了一個(gè)System.Web.UI.Page類的實(shí)例作為參數(shù),不會(huì)影響原來(lái)的程序代碼。
整個(gè)程序的代碼如下:
代碼如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;

/// <summary>
/// 一些常用的Js調(diào)用
/// 添加新版說(shuō)明:由于舊版普遍采用Response.Write(string msg)的方式輸出js腳本,這種
/// 方式輸出的js腳本會(huì)在html元素的<html></html>標(biāo)簽之外,破壞了整個(gè)xhtml的結(jié)構(gòu),
/// 而新版本則采用ClientScript.RegisterStartupScript(string msg)的方式輸出,不會(huì)改變xhtml的結(jié)構(gòu),
/// 不會(huì)影響執(zhí)行效果。
/// 為了向下兼容,所以新版本采用了重載的方式,新版本中要求一個(gè)System.Web.UI.Page類的實(shí)例。
/// 創(chuàng)建者:馬先光
/// 新版作者:周公
/// 修改版發(fā)布網(wǎng)址: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>
    /// 彈出消息框并且轉(zhuǎn)向到新的URL
    /// </summary>
    /// <param name="message">消息內(nèi)容</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>
    /// 回到歷史頁(yè)面
    /// </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>
    /// 關(guān)閉當(dāng)前窗口
    /// </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>
    /// 刷新打開(kāi)窗口
    /// </summary>
    public static void RefreshOpener()
    {
        #region
        string js = @"<Script language='JavaScript'>
                    opener.location.reload();
                  </Script>";
        HttpContext.Current.Response.Write(js);
        #endregion
    }


    /// <summary>
    /// 打開(kāi)指定大小的新窗體
    /// </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>
    /// 轉(zhuǎn)向Url制定的頁(yè)面
    /// </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>
    /// 打開(kāi)指定大小位置的模式對(duì)話框
    /// </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>
    /// 彈出模態(tài)窗口
    /// </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>
    /// 彈出模態(tài)窗口
    /// </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>
    /// 彈出消息框并且轉(zhuǎn)向到新的URL
    /// </summary>
    /// <param name="message">消息內(nèi)容</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>
    /// 回到歷史頁(yè)面
    /// </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>
    //        /// 關(guān)閉當(dāng)前窗口
    //        /// </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>
    /// 刷新打開(kāi)窗口
    /// </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>
    /// 打開(kāi)指定大小的新窗體
    /// </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>
    /// 轉(zhuǎn)向Url制定的頁(yè)面
    /// </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>
    /// 打開(kāi)指定大小位置的模式對(duì)話框
    /// </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>
    /// 彈出模態(tài)窗口
    /// </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>
    //        /// 彈出模態(tài)窗口
    //        /// </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
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

ASP.NET中常用輸出JS腳本的類實(shí)例

ASP.NET中常用輸出JS腳本的類實(shí)例:本文實(shí)例講述了ASP.NET中常用輸出JS腳本的類,針對(duì)過(guò)去輸出js腳本的類進(jìn)行了一定的改進(jìn)。在項(xiàng)目開(kāi)發(fā)中非常具有實(shí)用價(jià)值。分享給大家供大家參考。具體如下: 很多時(shí)候在ASP.NET中我們經(jīng)常需要輸出一些JS腳本,比如彈出一個(gè)警告窗口,返回到歷史頁(yè)面等JS功能
推薦度:
標(biāo)簽: 常用 js 輸出
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top