An instance of an JS operation class implemented by C
- 2021-01-25 07:52:38
- OfStack
This article illustrates the JS action classes implemented by C#. Share with you for your reference. The details are as follows:
This C# class encapsulates common JS client code operations, including pop-up dialogs, back to the previous page, redirection through JS, warning boxes, and redirection, etc.
using System.Web;
namespace DotNet.Utilities
{
/// <summary>
/// Client script output
/// </summary>
public class JsHelper
{
/// <summary>
/// Pop-up message , And jump to the specified page.
/// </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>
/// Pop-up message , And return to the history page
/// </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>
/// Jump directly to the specified page
/// </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>
/// Pop-up message And specify to the parent window
/// </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>
/// Return to the parent window
/// </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>
/// Back to the history page
/// </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>
/// Pop-up message
/// </summary>
public static void Alert(string message)
{
string js = "<script language=javascript>alert('{0}');</script>";
HttpContext.Current.Response.Write(string.Format(js, message));
}
/// <summary>
/// Register the script block
/// </summary>
public static void RegisterScriptBlock(System.Web.UI.Page page, string _ScriptString)
{
page.ClientScript.RegisterStartupScript(page.GetType(), "scriptblock", "<script type='text/javascript'>" + _ScriptString + "</script>");
}
}
}
I hope this article is helpful to your C# program design.