C#完成的JS操作類實例。本站提示廣大學習愛好者:(C#完成的JS操作類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成的JS操作類實例正文
本文實例講述了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#法式設計有所贊助。