JsHelper幫助類(lèi)實(shí)現(xiàn):

前端JS在后臺(tái)調(diào)用的C#幫助類(lèi)庫(kù)JSHelper源碼

輕松實(shí)現(xiàn)在C#后臺(tái)操作JS方法



public class JSHelper
    {
        /// <summary>
        /// 向客戶(hù)端寫(xiě)入js腳本
        /// </summary>
        /// <param name="script">js內(nèi)容</param>
        public static void RegisterScript(string script)
        {
            Page page = HttpContext.Current.Handler as Page;
            page.ClientScript.RegisterStartupScript(page.GetType(), "js2", string.Format("<script>{0}</script>", script));
        }
  
        /// <summary>
        /// JS彈出對(duì)消息話框
        /// </summary>
        /// <param name="message">要顯示的消息</param>
        public static void Alert(string message)
        {
            Page page = HttpContext.Current.Handler as Page;
            Guid gid = new Guid();
            string jsblock = "js"   gid.ToString();
            page.ClientScript.RegisterStartupScript(page.GetType(), jsblock, string.Format("<script>alert(\"{0}\")</script>", message));
        }
  
        /// <summary>
        /// JS跳轉(zhuǎn)指定URL
        /// </summary>
        /// <param name="url">要轉(zhuǎn)到的URL</param>
        public static void GoToUrl(string url)
        {
            RegisterScript(string.Format("location.href='{0}'", url));
        }
  
        /// <summary>
        /// JS刷新頁(yè)面
        /// </summary>
        public static void Refresh()
        {
            RegisterScript(string.Format ("location.reload();"));
        }
  
        public static void AlertAndRedirect(string message, string url)
        {
            Alert(message);
            GoToUrl(url);
        }
    }


原文鏈接:C#后端操作Js的幫助類(lèi)庫(kù)JsHelper源碼