ASP.NET JScript公共类(非常有用)
using System.Web;
public class JScript
{
/// <summary>
/// 弹出JavaScript小窗口
/// </summary>
public static void Alert(string message)
{
string js = "<script language='JavaScript'>alert('{0}');history.go(-1);</script>";
HttpContext.Current.Response.Write(string.Format(js, message));
}
/// <summary>
/// 弹出消息框并且转向到新的URL
/// </summary>
public static void Alert(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));
}
/// <summary>
/// 弹出消息框并且转向到指定FRAME的新URL
/// </summary>
public static void Alert(string message, string frame, string toURL)
{
string js = "<script language='JavaScript'>alert('{0}');{1}.location.replace('{2}')</script>";
HttpContext.Current.Response.Write(string.Format(js, message, frame, toURL));
}
/// <summary>
/// 回到历史页面
/// </summary>
public static void GoHistory(int value)
{
string js = "<script language='JavaScript'>history.go({0});</script>";
HttpContext.Current.Response.Write(string.Format(js, value));
}
/// <summary>
/// 关闭当前窗口
/// <summary>
public static void CloseWindow()
{
string js = "<script language='JavaScript'>parent.opener=null;window.close();</script>";
HttpContext.Current.Response.Write(js);
HttpContext.Current.Response.End();
}
/// <summary>
/// 父窗口打开指定页面
/// </summary>
public static void RefreshParent(string url)
{
string js = "<script language='JavaScript'>window.opener.location.href='{0}';window.close();</script>";
HttpContext.Current.Response.Write(string.Format(js, url));
}
/// <summary>
/// 刷新窗口
/// </summary>
public static void RefreshWindow()
{
string js = "<script language='JavaScript'>opener.location.reload();</script>";
HttpContext.Current.Response.Write(js);
}
/// <summary>
/// 打开指定大小的新窗体
/// </summary>
public static void OpenWindow(string url, int width, int heigth, int top, int left)
{
string js = "<script language='JavaScript'>window.open('{0}', '', 'width={1}, height={2}, top={3}, left={4}, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, titlebar=no, toolbar=no, directories=no');</script>";
HttpContext.Current.Response.Write(string.Format(js, url, width, heigth, top, left));
}
/// <summary>
/// 打开新窗体
/// </summary>
public static void OpenWindow(string url)
{
string js = "<script language='JavaScript'>window.open('{0}');</script>";
HttpContext.Current.Response.Write(string.Format(js, url));
}
/// <summary>
/// 转向指定窗口指定Url的页面
/// </summary>
public static void JsGoTo(string window, string url)
{
string js = "<script language='JavaScript'>{0}.location.replace('{1}');</script>";
HttpContext.Current.Response.Write(string.Format(js, window, url));
}
/// <summary>
/// 转向指定Url的页面
/// </summary>
public static void JavaScriptLocationHref(string url)
{
JsGoTo("window", url);
}
/// <summary>
/// 转向top指定Url的页面
/// </summary>
public static void TopGoTo(string url)
{
JsGoTo("top", url);
}
/// <summary>
/// 转向parent指定Url的页面
/// </summary>
public static void ParentGoTo(string url)
{
JsGoTo("parent", url);
}
/// <summary>
/// 打开指定大小位置的模式对话框
/// </summary>
public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left)
{
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);
}
/// <summary>
/// 打开模式窗口
/// </summary>
public static void ShowModalDialogWindow(string webFormUrl, string features)
{
string js = ShowModalDialogJavascript(webFormUrl, features);
HttpContext.Current.Response.Write(js);
}
/// <summary>
/// 返回模式窗口脚本
/// </summary>
public static string ShowModalDialogJavascript(string webFormUrl, string features)
{
string js = "<script language=javascript>showModalDialog('" + webFormUrl + "','','" + features + "');</script>";
return js;
}
/// <summary>
/// 自定义JS脚本
/// </summary>
public static void Jscript(string str)
{
HttpContext.Current.Response.Write("<script>" + str + "</script>");
}
}
public class JScript
{
/// <summary>
/// 弹出JavaScript小窗口
/// </summary>
public static void Alert(string message)
{
string js = "<script language='JavaScript'>alert('{0}');history.go(-1);</script>";
HttpContext.Current.Response.Write(string.Format(js, message));
}
/// <summary>
/// 弹出消息框并且转向到新的URL
/// </summary>
public static void Alert(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));
}
/// <summary>
/// 弹出消息框并且转向到指定FRAME的新URL
/// </summary>
public static void Alert(string message, string frame, string toURL)
{
string js = "<script language='JavaScript'>alert('{0}');{1}.location.replace('{2}')</script>";
HttpContext.Current.Response.Write(string.Format(js, message, frame, toURL));
}
/// <summary>
/// 回到历史页面
/// </summary>
public static void GoHistory(int value)
{
string js = "<script language='JavaScript'>history.go({0});</script>";
HttpContext.Current.Response.Write(string.Format(js, value));
}
/// <summary>
/// 关闭当前窗口
/// <summary>
public static void CloseWindow()
{
string js = "<script language='JavaScript'>parent.opener=null;window.close();</script>";
HttpContext.Current.Response.Write(js);
HttpContext.Current.Response.End();
}
/// <summary>
/// 父窗口打开指定页面
/// </summary>
public static void RefreshParent(string url)
{
string js = "<script language='JavaScript'>window.opener.location.href='{0}';window.close();</script>";
HttpContext.Current.Response.Write(string.Format(js, url));
}
/// <summary>
/// 刷新窗口
/// </summary>
public static void RefreshWindow()
{
string js = "<script language='JavaScript'>opener.location.reload();</script>";
HttpContext.Current.Response.Write(js);
}
/// <summary>
/// 打开指定大小的新窗体
/// </summary>
public static void OpenWindow(string url, int width, int heigth, int top, int left)
{
string js = "<script language='JavaScript'>window.open('{0}', '', 'width={1}, height={2}, top={3}, left={4}, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, titlebar=no, toolbar=no, directories=no');</script>";
HttpContext.Current.Response.Write(string.Format(js, url, width, heigth, top, left));
}
/// <summary>
/// 打开新窗体
/// </summary>
public static void OpenWindow(string url)
{
string js = "<script language='JavaScript'>window.open('{0}');</script>";
HttpContext.Current.Response.Write(string.Format(js, url));
}
/// <summary>
/// 转向指定窗口指定Url的页面
/// </summary>
public static void JsGoTo(string window, string url)
{
string js = "<script language='JavaScript'>{0}.location.replace('{1}');</script>";
HttpContext.Current.Response.Write(string.Format(js, window, url));
}
/// <summary>
/// 转向指定Url的页面
/// </summary>
public static void JavaScriptLocationHref(string url)
{
JsGoTo("window", url);
}
/// <summary>
/// 转向top指定Url的页面
/// </summary>
public static void TopGoTo(string url)
{
JsGoTo("top", url);
}
/// <summary>
/// 转向parent指定Url的页面
/// </summary>
public static void ParentGoTo(string url)
{
JsGoTo("parent", url);
}
/// <summary>
/// 打开指定大小位置的模式对话框
/// </summary>
public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left)
{
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);
}
/// <summary>
/// 打开模式窗口
/// </summary>
public static void ShowModalDialogWindow(string webFormUrl, string features)
{
string js = ShowModalDialogJavascript(webFormUrl, features);
HttpContext.Current.Response.Write(js);
}
/// <summary>
/// 返回模式窗口脚本
/// </summary>
public static string ShowModalDialogJavascript(string webFormUrl, string features)
{
string js = "<script language=javascript>showModalDialog('" + webFormUrl + "','','" + features + "');</script>";
return js;
}
/// <summary>
/// 自定义JS脚本
/// </summary>
public static void Jscript(string str)
{
HttpContext.Current.Response.Write("<script>" + str + "</script>");
}
}