当前位置:编程学习 > ASP.NET> 正文

asp.net后台输出js脚本

时间:2013-12-27类别:编程学习

asp.net后台输出js脚本

asp.net后台输出js脚本

Response.Write的写法

  •  
  • C# 代码   复制
  •     
        /// <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>");
            }
        }
    
    		
  •  

    page.ClientScript.RegisterStartupScript方式

  •  
  • C# 代码   复制
  • 
        /// <summary>
        /// 弹出JavaScript小窗口
        /// </summary>
        /// <param name="js">窗口信息</param>
        public static void Alert(string message, Page page)
        {
            ...
        }
    
        /// <summary>
        /// 弹出消息框并且转向到新的URL
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="toURL">连接地址</param>
        public static void AlertAndRedirect(string message, string toURL, Page page)
        {
            ...
        }
    
        /// <summary>
        /// 回到历史页面
        /// </summary>
        /// <param name="value">-1/1</param>
        public static void GoHistory(int value, Page page)
        {
            ...
        }
    
    标签:
  • 上一篇下一篇

    猜您喜欢

    热门推荐