Asp.net实用技巧(1)

博客围绕JavaScript展开,介绍了在点击Button时打开新页面的方法,将其封装成API。还提及打开固定大小页面的API,以及在关闭当前页面时刷新“父页面”的API,满足不同应用场景需求。

1.在新窗口中打开页面
我们经常需要在点击某个Button的时候打开一个新的页面,而且由于应用的需要,我们又不能使用超级连接或者LinkButton来代替这个Button,于是我们只有在Button的Click事件中进行新页面的打开工作。我将这个工作封装成一个API,如下:

1OpenWindowInNewPage#regionOpenWindowInNewPage
2//在新窗口中打开页面
3publicstaticvoidOpenWindowInNewPage(PagecurPage,stringdestUrl)
4{
5stringscriptString=string.Format("<scriptlanguage='JavaScript'>window.open('"+"{0}"+"','_new');<",destUrl);
6scriptString+="/";
7scriptString+="script>";
8if(!curPage.IsStartupScriptRegistered("Startup"))
9{
10curPage.RegisterStartupScript("Startup",scriptString);
11}

12}

13#endregion


2.如果需要打开固定大小的页面,可以使用如下API

1OpenNewFixSizePage#regionOpenNewFixSizePage
2//打开一个固定大小的页面,如果fullScreen为true,则high与width不起作用
3publicstaticvoidOpenNewFixSizePage(Pagepage,stringpageUrl,boolisCloseOldPage,stringscriptName,boolfullScreen,inthigh,intwidth)
4{
5StringBuilderStrScript=newStringBuilder();
6StrScript.Append("<scriptlanguage=javascript>");
7if(fullScreen)
8{
9StrScript.Append("width=screen.Width-10;"+"/n");
10StrScript.Append("height=screen.height-60;"+"/n");
11}

12else
13{
14StrScript.Append(string.Format("width={0};",width)+"/n");
15StrScript.Append(string.Format("height={0};",high)+"/n");
16}

17
18StrScript.Append("window.open('"+pageUrl+"','_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,top=0,left=0,height='+height+',width='+width+'');");
19if(isCloseOldPage)
20{
21StrScript.Append("window.focus();");
22StrScript.Append("window.opener=null;");
23StrScript.Append("window.close();");
24}

25StrScript.Append("</script>");
26if(!page.IsStartupScriptRegistered(scriptName))
27{
28page.RegisterStartupScript(scriptName,StrScript.ToString());
29}

30}

31#endregion

3.还有一种情况就是我们需要在关闭当前页面时,刷新当前页面的“父页面”,所谓“父页面”,就是Post本页面之前的一个页面。可以调用如下API:

RefreshFatherPage#regionRefreshFatherPage
//刷新Father页面
publicstaticvoidRefreshFatherPage(HttpResponseResponse,boolisCloseCurPage)
{
StringBuilderscriptString
=newStringBuilder();
scriptString.Append(
"<scriptlanguage=javascript>");
scriptString.Append(
"window.opener.refresh();");
if(isCloseCurPage)
{
scriptString.Append(
"window.focus();");
scriptString.Append(
"window.opener=null;");
scriptString.Append(
"window.close();");
}

scriptString.Append(
"</"+"script>");
Response.Write(scriptString.ToString());
}


/**//*
需要在Father页面的html中添加如下脚本(在Header中):
<scriptlanguage="javascript">
functionrefresh()
{
this.location=this.location;
}
</script>
*/

#endregion

以前一直都是做WindowsForm,现在因为公司的项目需要,不得不研究WebForm,以上的几个技巧是这几天解决工作中的问题而总结出来的,如有更好的办法,还请留言告诉我,谢谢!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值