SoFunction
Updated on 2025-03-07

JS operation class instance implemented by C#

This article describes the JS operation class implemented by C#. Share it for your reference. The details are as follows:

This C# class encapsulates commonly used JS client code operations, including popping up a dialog box, returning to the previous page, turning through JS, popping up a warning box and turning, etc.

using ;
namespace 
{
  /// <summary>
  /// Client script output  /// </summary>
  public class JsHelper
  {
    /// <summary>
    /// The message pops up and jumps to the specified page.    /// </summary>
    public static void AlertAndRedirect(string message, string toURL)
    {
      string js = "<script language=javascript>alert('{0}');('{1}')</script>";
      ((js, message, toURL));
      ();
    }
    /// <summary>
    /// Pop up information and return to the historical page    /// </summary>
    public static void AlertAndGoHistory(string message, int value)
    {
      string js = @"<Script language='JavaScript'>alert('{0}');({1});</Script>";
      ((js, message, value));
      ();
    }
    /// <summary>
    /// Jump directly to the specified page    /// </summary>
    public static void Redirect(string toUrl)
    {
      string js = @"<script language=javascript>('{0}')</script>";
      ((js, toUrl));
    }
    /// <summary>
    /// pop-up information and specify it to the parent window    /// </summary>
    public static void AlertAndParentUrl(string message, string toURL)
    {
      string js = "<script language=javascript>alert('{0}');('{1}')</script>";
      ((js, message, toURL));
    }
    /// <summary>
    /// Return to parent window    /// </summary>
    public static void ParentRedirect(string ToUrl)
    {
      string js = "<script language=javascript>('{0}')</script>";
      ((js, ToUrl));
    }
    /// <summary>
    /// Return to the history page    /// </summary>
    public static void BackHistory(int value)
    {
      string js = @"<Script language='JavaScript'>({0});</Script>";
      ((js, value));
      ();
    }
    /// <summary>
    /// pop-up information    /// </summary>
    public static void Alert(string message)
    {
      string js = "<script language=javascript>alert('{0}');</script>";
      ((js, message));
    }
    /// <summary>
    /// Register script block    /// </summary>
    public static void RegisterScriptBlock( page, string _ScriptString)
    {
      ((), "scriptblock", "<script type='text/javascript'>" + _ScriptString + "</script>");
    }
  }
}

I hope this article will be helpful to everyone's C# programming.