C#实现的JS操作类实例
本文实例讲述了C#实现的JS操作类。分享给大家供大家参考。具体如下:
这个C#类封装了常用的JS客户端代码操作,包括弹出对话框、返回上一页,通过JS转向,弹出警告框并转向等。
usingSystem.Web; namespaceDotNet.Utilities { ///<summary> ///客户端脚本输出 ///</summary> publicclassJsHelper { ///<summary> ///弹出信息,并跳转指定页面。 ///</summary> publicstaticvoidAlertAndRedirect(stringmessage,stringtoURL) { stringjs="<scriptlanguage=javascript>alert('{0}');window.location.replace('{1}')</script>"; HttpContext.Current.Response.Write(string.Format(js,message,toURL)); HttpContext.Current.Response.End(); } ///<summary> ///弹出信息,并返回历史页面 ///</summary> publicstaticvoidAlertAndGoHistory(stringmessage,intvalue) { stringjs=@"<Scriptlanguage='JavaScript'>alert('{0}');history.go({1});</Script>"; HttpContext.Current.Response.Write(string.Format(js,message,value)); HttpContext.Current.Response.End(); } ///<summary> ///直接跳转到指定的页面 ///</summary> publicstaticvoidRedirect(stringtoUrl) { stringjs=@"<scriptlanguage=javascript>window.location.replace('{0}')</script>"; HttpContext.Current.Response.Write(string.Format(js,toUrl)); } ///<summary> ///弹出信息并指定到父窗口 ///</summary> publicstaticvoidAlertAndParentUrl(stringmessage,stringtoURL) { stringjs="<scriptlanguage=javascript>alert('{0}');window.top.location.replace('{1}')</script>"; HttpContext.Current.Response.Write(string.Format(js,message,toURL)); } ///<summary> ///返回到父窗口 ///</summary> publicstaticvoidParentRedirect(stringToUrl) { stringjs="<scriptlanguage=javascript>window.top.location.replace('{0}')</script>"; HttpContext.Current.Response.Write(string.Format(js,ToUrl)); } ///<summary> ///返回历史页面 ///</summary> publicstaticvoidBackHistory(intvalue) { stringjs=@"<Scriptlanguage='JavaScript'>history.go({0});</Script>"; HttpContext.Current.Response.Write(string.Format(js,value)); HttpContext.Current.Response.End(); } ///<summary> ///弹出信息 ///</summary> publicstaticvoidAlert(stringmessage) { stringjs="<scriptlanguage=javascript>alert('{0}');</script>"; HttpContext.Current.Response.Write(string.Format(js,message)); } ///<summary> ///注册脚本块 ///</summary> publicstaticvoidRegisterScriptBlock(System.Web.UI.Pagepage,string_ScriptString) { page.ClientScript.RegisterStartupScript(page.GetType(),"scriptblock","<scripttype='text/javascript'>"+_ScriptString+"</script>"); } } }
希望本文所述对大家的C#程序设计有所帮助。