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

asp.net后台如何输出js脚本使用什么方法可以实现

程序员文章站 2024-02-25 19:04:03
用page.clientscript.registerstartupscript方式实现 代码为: 复制代码 代码如下: /**//// /...
用page.clientscript.registerstartupscript方式实现

代码为:
复制代码 代码如下:

/**//// <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 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);
}
}