JsHelper帮助类实现:
前端JS在后台调用的C#帮助类库JSHelper源码
轻松实现在C#后台操作JS方法
public class JSHelper { /// <summary> /// 向客户端写入js脚本 /// </summary> /// <param name="script">js内容</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弹出对消息话框 /// </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跳转指定URL /// </summary> /// <param name="url">要转到的URL</param> public static void GoToUrl(string url) { RegisterScript(string.Format("location.href='{0}'", url)); } /// <summary> /// JS刷新页面 /// </summary> public static void Refresh() { RegisterScript(string.Format ("location.reload();")); } public static void AlertAndRedirect(string message, string url) { Alert(message); GoToUrl(url); } }