`
caobihole
  • 浏览: 941411 次
文章分类
社区版块
存档分类
最新评论

ASP.NET中常用输出JS脚本的类(改进版)

 
阅读更多

在ASP.NET中我们经常需要输出一些JS脚本,比如弹出一个警告窗口,返回到历史页面等JS功能,我看到网上流传得比较广的是马先光写的一个JScript类,这个类基本将经常用到的JS脚本包含了,非常方便,唯一的不足是作者采用的Response.Write(string msg)的办法,这样造成输出的js脚本在<html></html>标签之外,破坏了原有XHTML的结构,所以本人在满足原功能的情况下,对JScript类做了进一步的改善,这个改善采用了重载的办法,增加了一个System.Web.UI.Page类的实例作为参数,不会影响原来的程序代码。

整个程序的代码如下:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Web;
usingSystem.Web.UI;

///<summary>
///一些常用的Js调用
///添加新版说明:由于旧版普遍采用Response.Write(stringmsg)的方式输出js脚本,这种
///方式输出的js脚本会在html元素的&lt;html&gt;&lt;/html&gt;标签之外,破坏了整个xhtml的结构,
///而新版本则采用ClientScript.RegisterStartupScript(stringmsg)的方式输出,不会改变xhtml的结构,
///不会影响执行效果。
///为了向下兼容,所以新版本采用了重载的方式,新版本中要求一个System.Web.UI.Page类的实例。
///创建时间:2006-9-13
///创建者:马先光
///新版作者:周公
///修改日期:2007-4-17
///修改版发布网址:http://blog.csdn.net/zhoufoxcn
///</summary>
publicclassJScript
{
#region旧版本
///<summary>
///弹出JavaScript小窗口
///</summary>
///<paramname="js">窗口信息</param>
publicstaticvoidAlert(stringmessage)
{
#region
stringjs=@"<Scriptlanguage='JavaScript'>
alert('
"+message+"');</Script>";
HttpContext.Current.Response.Write(js);
#endregion
}

///<summary>
///弹出消息框并且转向到新的URL
///</summary>
///<paramname="message">消息内容</param>
///<paramname="toURL">连接地址</param>
publicstaticvoidAlertAndRedirect(stringmessage,stringtoURL)
{
#region
stringjs="<scriptlanguage=javascript>alert('{0}');window.location.replace('{1}')</script>";
HttpContext.Current.Response.Write(
string.Format(js,message,toURL));
#endregion
}

///<summary>
///回到历史页面
///</summary>
///<paramname="value">-1/1</param>
publicstaticvoidGoHistory(intvalue)
{
#region
stringjs=@"<Scriptlanguage='JavaScript'>
history.go({0});
</Script>
";
HttpContext.Current.Response.Write(
string.Format(js,value));
#endregion
}

///<summary>
///关闭当前窗口
///</summary>
publicstaticvoidCloseWindow()
{
#region
stringjs=@"<Scriptlanguage='JavaScript'>
parent.opener=null;window.close();
</Script>
";
HttpContext.Current.Response.Write(js);
HttpContext.Current.Response.End();
#endregion
}

///<summary>
///刷新父窗口
///</summary>
publicstaticvoidRefreshParent(stringurl)
{
#region
stringjs=@"<Scriptlanguage='JavaScript'>
window.opener.location.href='
"+url+"';window.close();</Script>";
HttpContext.Current.Response.Write(js);
#endregion
}


///<summary>
///刷新打开窗口
///</summary>
publicstaticvoidRefreshOpener()
{
#region
stringjs=@"<Scriptlanguage='JavaScript'>
opener.location.reload();
</Script>
";
HttpContext.Current.Response.Write(js);
#endregion
}


///<summary>
///打开指定大小的新窗体
///</summary>
///<paramname="url">地址</param>
///<paramname="width"></param>
///<paramname="heigth"></param>
///<paramname="top">头位置</param>
///<paramname="left">左位置</param>
publicstaticvoidOpenWebFormSize(stringurl,intwidth,intheigth,inttop,intleft)
{
#region
stringjs=@"<Scriptlanguage='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
}


///<summary>
///转向Url制定的页面
///</summary>
///<paramname="url">连接地址</param>
publicstaticvoidJavaScriptLocationHref(stringurl)
{
#region
stringjs=@"<Scriptlanguage='JavaScript'>
window.location.replace('{0}');
</Script>
";
js
=string.Format(js,url);
HttpContext.Current.Response.Write(js);
#endregion
}

///<summary>
///打开指定大小位置的模式对话框
///</summary>
///<paramname="webFormUrl">连接地址</param>
///<paramname="width"></param>
///<paramname="height"></param>
///<paramname="top">距离上位置</param>
///<paramname="left">距离左位置</param>
publicstaticvoidShowModalDialogWindow(stringwebFormUrl,intwidth,intheight,inttop,intleft)
{
#region
stringfeatures="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
}
///<summary>
///弹出模态窗口
///</summary>
///<paramname="webFormUrl"></param>
///<paramname="features"></param>
publicstaticvoidShowModalDialogWindow(stringwebFormUrl,stringfeatures)
{
stringjs=ShowModalDialogJavascript(webFormUrl,features);
HttpContext.Current.Response.Write(js);
}
///<summary>
///弹出模态窗口
///</summary>
///<paramname="webFormUrl"></param>
///<paramname="features"></param>
///<returns></returns>
publicstaticstringShowModalDialogJavascript(stringwebFormUrl,stringfeatures)
{
#region
stringjs=@"<scriptlanguage=javascript>
showModalDialog('
"+webFormUrl+"','','"+features+"');</script>";
returnjs;
#endregion
}
#endregion

#region新版本
///<summary>
///弹出JavaScript小窗口
///</summary>
///<paramname="js">窗口信息</param>
publicstaticvoidAlert(stringmessage,Pagepage)
{
#region
stringjs=@"<Scriptlanguage='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>
///<paramname="message">消息内容</param>
///<paramname="toURL">连接地址</param>
publicstaticvoidAlertAndRedirect(stringmessage,stringtoURL,Pagepage)
{
#region
stringjs="<scriptlanguage=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>
///<paramname="value">-1/1</param>
publicstaticvoidGoHistory(intvalue,Pagepage)
{
#region
stringjs=@"<Scriptlanguage='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>
//publicstaticvoidCloseWindow()
//{
//#region
//stringjs=@"<Scriptlanguage='JavaScript'>
//parent.opener=null;window.close();
//</Script>";
//HttpContext.Current.Response.Write(js);
//HttpContext.Current.Response.End();
//#endregion
//}

///<summary>
///刷新父窗口
///</summary>
publicstaticvoidRefreshParent(stringurl,Pagepage)
{
#region
stringjs=@"<Scriptlanguage='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>
publicstaticvoidRefreshOpener(Pagepage)
{
#region
stringjs=@"<Scriptlanguage='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>
///<paramname="url">地址</param>
///<paramname="width"></param>
///<paramname="heigth"></param>
///<paramname="top">头位置</param>
///<paramname="left">左位置</param>
publicstaticvoidOpenWebFormSize(stringurl,intwidth,intheigth,inttop,intleft,Pagepage)
{
#region
stringjs=@"<Scriptlanguage='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>
///<paramname="url">连接地址</param>
publicstaticvoidJavaScriptLocationHref(stringurl,Pagepage)
{
#region
stringjs=@"<Scriptlanguage='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>
///<paramname="webFormUrl">连接地址</param>
///<paramname="width"></param>
///<paramname="height"></param>
///<paramname="top">距离上位置</param>
///<paramname="left">距离左位置</param>
publicstaticvoidShowModalDialogWindow(stringwebFormUrl,intwidth,intheight,inttop,intleft,Pagepage)
{
#region
stringfeatures="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>
///<paramname="webFormUrl"></param>
///<paramname="features"></param>
publicstaticvoidShowModalDialogWindow(stringwebFormUrl,stringfeatures,Pagepage)
{
stringjs=ShowModalDialogJavascript(webFormUrl,features);
//HttpContext.Current.Response.Write(js);
if(!page.ClientScript.IsStartupScriptRegistered(page.GetType(),"ShowModalDialogWindow"))
{
page.ClientScript.RegisterStartupScript(page.GetType(),
"ShowModalDialogWindow",js);
}
}
/////<summary>
/////弹出模态窗口
/////</summary>
/////<paramname="webFormUrl"></param>
/////<paramname="features"></param>
/////<returns></returns>
//publicstaticstringShowModalDialogJavascript(stringwebFormUrl,stringfeatures)
//{
//#region
//stringjs=@"<scriptlanguage=javascript>
//showModalDialog('"+webFormUrl+"','','"+features+"');</script>";
//returnjs;
//#endregion
//}
#endregion
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics