1. 程式人生 > >C#實現的JS操作類實例

C#實現的JS操作類實例

www. ora play gpo cursor help tran scrip 文件

本文實例講述了C#實現的JS操作類。分享給大家供大家參考。具體如下:

這個C#類封裝了常用的JS客戶端代碼操作,包括彈出對話框、返回上一頁,通過JS轉向,彈出警告框並轉向等。

using System.Web;
namespace DotNet.Utilities
{
  /// <summary>
  /// 客戶端腳本輸出
  /// </summary>
  public class JsHelper
  {
    /// <summary>
    /// 彈出信息,並跳轉指定頁面。
    /// </summary>
    public static void AlertAndRedirect(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));
      HttpContext.Current.Response.End();
    }
    /// <summary>
    /// 彈出信息,並返回歷史頁面
    /// </summary>
    public static void AlertAndGoHistory(string message, int value)
    {
      string js = @"<Script language=‘JavaScript‘>alert(‘{0}‘);history.go({1});</Script>";
      HttpContext.Current.Response.Write(string.Format(js, message, value));
      HttpContext.Current.Response.End();
    }
    /// <summary>
    /// 直接跳轉到指定的頁面
    /// </summary>
    public static void Redirect(string toUrl)
    {
      string js = @"<script language=javascript>window.location.replace(‘{0}‘)</script>";
      HttpContext.Current.Response.Write(string.Format(js, toUrl));
    }
    /// <summary>
    /// 彈出信息 並指定到父窗口
    /// </summary>
    public static void AlertAndParentUrl(string message, string toURL)
    {
      string js = "<script language=javascript>alert(‘{0}‘);window.top.location.replace(‘{1}‘)</script>";
      HttpContext.Current.Response.Write(string.Format(js, message, toURL));
    }
    /// <summary>
    /// 返回到父窗口
    /// </summary>
    public static void ParentRedirect(string ToUrl)
    {
      string js = "<script language=javascript>window.top.location.replace(‘{0}‘)</script>";
      HttpContext.Current.Response.Write(string.Format(js, ToUrl));
    }
    /// <summary>
    /// 返回歷史頁面
    /// </summary>
    public static void BackHistory(int value)
    {
      string js = @"<Script language=‘JavaScript‘>history.go({0});</Script>";
      HttpContext.Current.Response.Write(string.Format(js, value));
      HttpContext.Current.Response.End();
    }
    /// <summary>
    /// 彈出信息
    /// </summary>
    public static void Alert(string message)
    {
      string js = "<script language=javascript>alert(‘{0}‘);</script>";
      HttpContext.Current.Response.Write(string.Format(js, message));
    }
    /// <summary>
    /// 註冊腳本塊
    /// </summary>
    public static void RegisterScriptBlock(System.Web.UI.Page page, string _ScriptString)
    {
      page.ClientScript.RegisterStartupScript(page.GetType(), "scriptblock", "<script type=‘text/javascript‘>" + _ScriptString + "</script>");
    }
  }
}

希望本文所述對大家的C#程序設計有所幫助。

除聲明外,跑步客文章均為原創,轉載請以鏈接形式標明本文地址
C#實現的JS操作類實例

本文地址: http://www.paobuke.com/develop/c-develop/pbk23068.html






相關內容

技術分享圖片C# 實現截圖軟件功能實例代碼技術分享圖片C#實現Access通用訪問類OleDbHelper完整實例技術分享圖片C# 表達式樹Expression Trees的知識梳理技術分享圖片jQuery uploadify在谷歌和火狐瀏覽器上傳失敗的解決方案
技術分享圖片C#實現Menu和ContextMenu自定義風格及contextMenu自定義技術分享圖片C#如何打開並讀取usb的文件目錄技術分享圖片
C#字符串數組轉換為整形數組的方法技術分享圖片C#實現操作MySql數據層類MysqlHelper實例

C#實現的JS操作類實例