ASP.net的一点小技巧【转自qddn】
来源:互联网 发布:mutil paxos算法 编辑:程序博客网 时间:2024/06/06 02:53
ASP.net在做一些项目时,可能会遇到一鞋小麻烦,下面是几个使用ASP.net的小技巧:
1.在新窗口中打开页面
我们经常需要在点击某个Button的时候打开一个新的页面,而且由于应用的需要,我们又不能使用超级连接或者LinkButton来代替这个Button,于是我们只有在Button的Click事件中进行新页面的打开工作。我将这个工作封装成一个API,如下:
1OpenWindowInNewPage#region OpenWindowInNewPage
2 //在新窗口中打开页面
3 public static void OpenWindowInNewPage(Page curPage ,string destUrl)
4 {
5 string scriptString = string.Format("window.open('" + "{0}" + "','_new');<" ,destUrl) ;
6 scriptString += "/";
7 scriptString += "script>";
8 if(!curPage.IsStartupScriptRegistered("Startup"))
9 {
10 curPage.RegisterStartupScript("Startup", scriptString);
11 }
12 }
13 #endregion
2 //在新窗口中打开页面
3 public static void OpenWindowInNewPage(Page curPage ,string destUrl)
4 {
5 string scriptString = string.Format("window.open('" + "{0}" + "','_new');<" ,destUrl) ;
6 scriptString += "/";
7 scriptString += "script>";
8 if(!curPage.IsStartupScriptRegistered("Startup"))
9 {
10 curPage.RegisterStartupScript("Startup", scriptString);
11 }
12 }
13 #endregion
2.如果需要打开固定大小的页面,可以使用如下API
1OpenNewFixSizePage#region OpenNewFixSizePage
2 //打开一个固定大小的页面,如果fullScreen为true ,则high与width不起作用
3 public static void OpenNewFixSizePage(Page page, string pageUrl, bool isCloseOldPage, string scriptName ,bool fullScreen ,int high ,int width)
4 {
5 StringBuilder StrScript = new StringBuilder();
6 StrScript.Append( "" );
7 if(fullScreen)
8 {
9 StrScript.Append("width=screen.Width-10;"+"/n");
10 StrScript.Append("height=screen.height-60;"+"/n");
11 }
12 else
13 {
14 StrScript.Append(string.Format("width={0};" ,width)+"/n");
15 StrScript.Append(string.Format("height={0};" ,high)+"/n");
16 }
17
18 StrScript.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 +'');" );
19 if ( isCloseOldPage )
20 {
21 StrScript.Append( " window.focus();" );
22 StrScript.Append( " window.opener=null;" );
23 StrScript.Append( " window.close(); " );
24 }
25 StrScript.Append( "" );
26 if ( ! page.IsStartupScriptRegistered( scriptName ) )
27 {
28 page.RegisterStartupScript( scriptName, StrScript.ToString() );
29 }
30 }
31 #endregion
2 //打开一个固定大小的页面,如果fullScreen为true ,则high与width不起作用
3 public static void OpenNewFixSizePage(Page page, string pageUrl, bool isCloseOldPage, string scriptName ,bool fullScreen ,int high ,int width)
4 {
5 StringBuilder StrScript = new StringBuilder();
6 StrScript.Append( "" );
7 if(fullScreen)
8 {
9 StrScript.Append("width=screen.Width-10;"+"/n");
10 StrScript.Append("height=screen.height-60;"+"/n");
11 }
12 else
13 {
14 StrScript.Append(string.Format("width={0};" ,width)+"/n");
15 StrScript.Append(string.Format("height={0};" ,high)+"/n");
16 }
17
18 StrScript.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 +'');" );
19 if ( isCloseOldPage )
20 {
21 StrScript.Append( " window.focus();" );
22 StrScript.Append( " window.opener=null;" );
23 StrScript.Append( " window.close(); " );
24 }
25 StrScript.Append( "" );
26 if ( ! page.IsStartupScriptRegistered( scriptName ) )
27 {
28 page.RegisterStartupScript( scriptName, StrScript.ToString() );
29 }
30 }
31 #endregion
3.还有一种情况就是我们需要在关闭当前页面时,刷新当前页面的“父页面”,所谓“父页面”,就是Post本页面之前的一个页面。可以调用如下API:
RefreshFatherPage#region RefreshFatherPage
//刷新Father页面
public static void RefreshFatherPage(HttpResponse Response ,bool isCloseCurPage)
{
StringBuilder scriptString = new StringBuilder();
scriptString.Append("");
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中):
function refresh()
{
this.location = this.location;
}
*/
#endregion
//刷新Father页面
public static void RefreshFatherPage(HttpResponse Response ,bool isCloseCurPage)
{
StringBuilder scriptString = new StringBuilder();
scriptString.Append("");
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中):
function refresh()
{
this.location = this.location;
}
*/
#endregion
关于第3点有一些补冲。
可以定义一个JS文件增加下面的函数。
function OpenerReload()
{
if (opener && !opener.closed)
{
opener.Reload();
}
}
Reload()函数就是子窗体内的脚本函数,这样在子窗体的脚本函数里可以调用任何服务器端的函数,就不需要在not Ispostback里面处理刷新的机能了。
例如使用
function Reload() { document.forms(0).btnSearch.click(); }
父窗体调用的部分就可以写成Opener.OpenerReload();
- ASP.net的一点小技巧【转自qddn】
- ASP.net的一点小技巧【转自qddn】
- ASP.net的一点小技巧【转自qddn】
- 在ASP.NET使用javascript的一点小技巧
- 在ASP.NET使用javascript的一点小技巧
- 在ASP.NET使用javascript的一点小技巧
- 在ASP.NET使用javascript的一点小技巧
- 在ASP.NET使用javascript的一点小技巧
- 在ASP.NET使用javascript的一点小技巧
- 在ASP.NET使用javascript的一点小技巧
- 在ASP.NET使用javascript的一点小技巧(2)
- 在ASP.NET使用javascript的一点小技巧
- 在ASP.NET使用javascript的一点小技巧
- 在ASP.NET使用javascript的一点小技巧
- 在ASP.NET使用javascript的一点小技巧
- (转)Asp.net 2.0 关于Header/title/Meta tages/Style操作的一点小技巧
- Asp.net 2.0 关于Header/title/Meta tages/Style操作的一点小技巧
- Asp.net 2.0 关于Header/title/Meta tages/Style操作的一点小技巧
- 数码管显示数字之C语言模拟
- 查询跳转和表跳转
- 用 C 语言编写 Windows 服务程序的五个步骤
- google map
- FS-2工作室关于暂停以FS命名的公告
- ASP.net的一点小技巧【转自qddn】
- Ally 要结婚了!
- 寻址技术
- google map test
- MDA模型转换的情况
- 使用WebClient
- HTML中隐藏水平向或垂直滚动条
- 如何把网络共享目录映射为本地驱动器(续)
- ASP.NET2.0中将数据库字段中存储的文件显示出来