欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET中常用的用来输出JS脚本的类

程序员文章站 2024-03-08 22:02:34
整个程序的代码如下: 复制代码 代码如下: using system; using system.collections.generic; using system.tex...
整个程序的代码如下:
复制代码 代码如下:

using system;
using system.collections.generic;
using system.text;
using system.web;
using system.web.ui;
/// <summary>
/// 弹出javascript小窗口
/// </summary>
/// <param name="js">窗口信息</param>
public static void alert(string message, page page)
{
#region
string js = @"<script language='javascript'>
alert('" + message + "');</script>";
//httpcontext.current.response.write(js);
if (!page.clientscript.isstartupscriptregistered(page.gettype(), "alert"))
{
page.clientscript.registerstartupscript(page.gettype(), "alert", js);
}
#endregion
}

/// <summary>
/// 弹出消息框并且转向到新的url
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="tourl">连接地址</param>
public static void alertandredirect(string message, string tourl, page page)
{
#region
string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
//httpcontext.current.response.write(string.format(js, message, tourl));
if (!page.clientscript.isstartupscriptregistered(page.gettype(), "alertandredirect"))
{
page.clientscript.registerstartupscript(page.gettype(), "alertandredirect", string.format(js, message, tourl));
}
#endregion
}

/// <summary>
/// 回到历史页面
/// </summary>
/// <param name="value">-1/1</param>
public static void gohistory(int value, page page)
{
#region
string js = @"<script language='javascript'>
history.go({0});
</script>";
//httpcontext.current.response.write(string.format(js, value));
if (!page.clientscript.isstartupscriptregistered(page.gettype(), "gohistory"))
{
page.clientscript.registerstartupscript(page.gettype(), "gohistory", string.format(js, value));
}
#endregion
}

/// <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
}

/// <summary>
/// 刷新父窗口
/// </summary>
public static void refreshparent(string url, page page)
{
#region
string js = @"<script language='javascript'>
window.opener.location.href='" + url + "';window.close();</script>";
//httpcontext.current.response.write(js);
if (!page.clientscript.isstartupscriptregistered(page.gettype(), "refreshparent"))
{
page.clientscript.registerstartupscript(page.gettype(), "refreshparent", js);
}
#endregion
}


/// <summary>
/// 刷新打开窗口
/// </summary>
public static void refreshopener(page page)
{
#region
string js = @"<script language='javascript'>
opener.location.reload();
</script>";
//httpcontext.current.response.write(js);
if (!page.clientscript.isstartupscriptregistered(page.gettype(), "refreshopener"))
{
page.clientscript.registerstartupscript(page.gettype(), "refreshopener", js);
}
#endregion
}


/// <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, page page)
{
#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);
if (!page.clientscript.isstartupscriptregistered(page.gettype(), "openwebformsize"))
{
page.clientscript.registerstartupscript(page.gettype(), "openwebformsize", js);
}
#endregion
}


/// <summary>
/// 转向url制定的页面
/// </summary>
/// <param name="url">连接地址</param>
public static void javascriptlocationhref(string url, page page)
{
#region
string js = @"<script language='javascript'>
window.location.replace('{0}');
</script>";
js = string.format(js, url);
//httpcontext.current.response.write(js);
if (!page.clientscript.isstartupscriptregistered(page.gettype(), "javascriptlocationhref"))
{
page.clientscript.registerstartupscript(page.gettype(), "javascriptlocationhref", js);
}
#endregion
}

/// <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, page page)
{
#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, page);
#endregion
}
/// <summary>
/// 弹出模态窗口
/// </summary>
/// <param name="webformurl"></param>
/// <param name="features"></param>
public static void showmodaldialogwindow(string webformurl, string features, page page)
{
string js = showmodaldialogjavascript(webformurl, features);
//httpcontext.current.response.write(js);
if (!page.clientscript.isstartupscriptregistered(page.gettype(), "showmodaldialogwindow"))
{
page.clientscript.registerstartupscript(page.gettype(), "showmodaldialogwindow", 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
}