/// <summary>
/// Js操作类
/// </summary>
public class JsHelper
{
    #region 页面JS操作
    /// <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>");
    }
    #region 回到历史页面
    /// <summary>
    /// 回到历史页面
    /// </summary>
    /// <param name="value">-1/1</param>
    public static void GoHistory(int value)
    {
        #region
        string js = @"<Script language='JavaScript'>
                history.go({0});  
                </Script>";
        HttpContext.Current.Response.Write(string.Format(js, value));
        #endregion
    }
    #endregion
    #region 关闭当前窗口
    /// <summary>
    /// 关闭当前窗口
    /// </summary>
    public static void CloseWindow()
    {
        #region
        string js = @"<Script language='JavaScript'>
                parent.opener=null;window.close();  
                </Script>";
        HttpContext.Current.Response.Write(js);
        HttpContext.Current.Response.End();
        #endregion
    }
    #endregion
    #region 刷新父窗口
    /// <summary>
    /// 刷新父窗口
    /// </summary>
    public static void RefreshParent(string url)
    {
        #region
        string js = @"<script>try{top.location=""" + url + @"""}catch(e){location=""" + url + @"""}</script>";
        HttpContext.Current.Response.Write(js);
        #endregion
    }
    #endregion
    #region 刷新打开窗口
    /// <summary>
    /// 刷新打开窗口
    /// </summary>
    public static void RefreshOpener()
    {
        #region
        string js = @"<Script language='JavaScript'>
                opener.location.reload();
                </Script>";
        HttpContext.Current.Response.Write(js);
        #endregion
    }
    #endregion
    #region 转向Url指定的页面
    /// <summary>
    /// 转向Url指定的页面
    /// </summary>
    /// <param name="url">连接地址</param>
    public static void JavaScriptLocationHref(string url)
    {
        #region
        string js = @"<Script language='JavaScript'>
                window.location.replace('{0}');
                </Script>";
        js = string.Format(js, url);
        HttpContext.Current.Response.Write(js);
        #endregion
    }
    #endregion
    #region 打开指定大小位置的模式对话框
    /// <summary>
    /// 打开指定大小位置的模式对话框
    /// </summary>
    /// <param name="webFormUrl">连接地址</param>
    /// <param name="width">宽</param>
    /// <param name="height">高</param>
    /// <param name="top">距离上位置</param>
    /// <param name="left">距离左位置</param>
    public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left)
    {
        #region
        string features = "dialogWidth:" + width.ToString() + "px"
            + ";dialogHeight:" + height.ToString() + "px"
            + ";dialogLeft:" + left.ToString() + "px"
            + ";dialogTop:" + top.ToString() + "px"
            + ";center:yes;help=no;resizable:no;status:no;scroll=yes";
        ShowModalDialogWindow(webFormUrl, features);
        #endregion
    }
    #endregion
    #region 打开模式对话框
    /// <summary>
    /// 打开模式对话框
    /// </summary>
    /// <param name="webFormUrl">链接地址</param>
    /// <param name="features"></param>
    public static void ShowModalDialogWindow(string webFormUrl, string features)
    {
        string js = ShowModalDialogJavascript(webFormUrl, features);
        HttpContext.Current.Response.Write(js);
    }
    /// <summary>
    /// 打开模式对话框
    /// </summary>
    /// <param name="webFormUrl"></param>
    /// <param name="features"></param>
    /// <returns></returns>
    public static string ShowModalDialogJavascript(string webFormUrl, string features)
    {
        #region
        string js = @"<script language=javascript>
showModalDialog('" + webFormUrl + "','','" + features + "');</script>";
        return js;
        #endregion
    }
    #endregion
    #region 打开指定大小的新窗体
    /// <summary>
    /// 打开指定大小的新窗体
    /// </summary>
    /// <param name="url">地址</param>
    /// <param name="width">宽</param>
    /// <param name="heigth">高</param>
    /// <param name="top">头位置</param>
    /// <param name="left">左位置</param>
    public static void OpenWebFormSize(string url, int width, int heigth, int top, int left)
    {
        #region
        string js = @"<Script language='JavaScript'>window.open('" + url + @"','','height=" + heigth + ",width=" + width + ",top=" + top + ",left=" + left + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');</Script>";
        HttpContext.Current.Response.Write(js);
        #endregion
    }
    #endregion
    /// <summary>
    /// 页面跳转(跳出框架)
    /// </summary>
    /// <param name="url"></param>
    public static void JavaScriptExitIfream(string url)
    {
        string js = @"<Script language='JavaScript'>
                parent.window.location.replace('{0}');
                </Script>";
        js = string.Format(js, url);
        HttpContext.Current.Response.Write(js);
    }
    #endregion
    #region JavaScript执行方法
    /// <summary>
    /// 执行 javascript 方法,以Object对象返回。
    /// </summary>
    /// <param name="JsCode">待执行的完整的 javascript 方法代码字符串。</param>
    /// <param name="FunctionName">要调用的方法名称,如:Fun1()、Fun2('a')、Fun3('1','2')等</param>
    /// <returns></returns>
    internal static object InvokeJS(string JsCode, string FunctionName)
    {
        try
        {
            Type obj = Type.GetTypeFromProgID("ScriptControl");
            if (obj == null) return null;
            object ScriptControl = Activator.CreateInstance(obj);
            obj.InvokeMember("Language", BindingFlags.SetProperty, null, ScriptControl, new object[] { "JScript" });
            obj.InvokeMember("AddCode", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { JsCode });
            return obj.InvokeMember("Eval", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { FunctionName });
        }
        catch (Exception ee)
        {
            return "";
        }
    }
    #endregion
}15
2015
07
      【C#、Asp.Net 工具类大全】Js常用操作类
            发布:郑德才博客 | 分类:项目源码 | 评论:0 | 浏览:
          
        相关文章:
C#、Asp.Net 对比两个实体信息前后是否有所改变(结构相同和不相同) (2016-9-2 20:24:29)
C#、Asp.Net 将一个实体对象转换为另一个实体对象(结构可以不一样) (2016-9-2 19:14:5)
Asp.Net 常用时间计算 (2016-3-16 23:29:20)
【C#、Asp.Net 工具类大全】图片通用操作类 (2015-12-23 13:27:30)
【C#、Asp.Net 工具类大全】Request请求工具类 (2015-12-23 13:15:56)
【C#、Asp.Net 工具类大全】弹出提示操作类 (2015-12-23 13:6:1)
【C#、Asp.Net 工具类大全】正则匹配工具类 (2015-7-26 9:36:56)
【C#、Asp.Net 工具类大全】Html常用帮助类 (2015-7-11 23:42:53)
C#字符串编码帮助类 (2015-7-11 23:39:38)
【C#、Asp.Net 工具类大全】压缩文本、字节或者文件的压缩辅助类 (2015-7-11 23:23:49)
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。