asp.net的一点代码

来源:互联网 发布:ui编程 编辑:程序博客网 时间:2024/06/05 08:38
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(""; 8 if(!curPage.IsStartupScriptRegistered("Startup")) 9 {10 curPage.RegisterStartupScript("Startup", scriptString);11 }12 }13 #endregion2.如果需要打开固定大小的页面,可以使用如下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 else13 {14 StrScript.Append(string.Format("width={0};" ,width)+"/n");15 StrScript.Append(string.Format("height={0};" ,high)+"/n"); 16 }1718 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(""); Response.Write(scriptString.ToString()); } /**//* 需要在Father页面的html中添加如下脚本(在Header中): */ #endregion 1. 在Asp.net实用技巧(1) 中提到了如何刷新父页面,那么如果要刷新父页面的父页面的父页面了?那就是刷新祖先页面RefreshAncestorPage。 RefreshAncestorPage#region RefreshAncestorPage /**//// /// 刷新指定的祖先页面,注意是"祖先页面" /// public static void RefreshAncestorPage(HttpResponse Response ,string targetPageTitle ,bool isCloseCurPage)//targetPageTitle 目标页面的title { StringBuilder scriptString = new StringBuilder(); scriptString.Append(""); scriptString.Append("var p = window ;"); scriptString.Append(string.Format("while(p.document.title != '{0}')" ,targetPageTitle)); scriptString.Append("{"); scriptString.Append("p = p.opener ;"); scriptString.Append("}"); scriptString.Append("p.focus();"); scriptString.Append("p.refresh();"); if (isCloseCurPage ) { scriptString.Append( " window.focus();" ); scriptString.Append( " window.opener=null;" ); scriptString.Append( " window.close(); " ); } scriptString.Append(""); Response.Write(scriptString.ToString()); } /**//* 需要在Father页面的html中添加如下脚本(在Header中): */ #endregion 2.如何刷新祖先页面中的某个frame中的page了? RefreshFrameInAncestorPage#region RefreshFrameInAncestorPage /**//// /// 刷新指定的祖先页面中的某个框架的内部页面 /// public static void RefreshFrameInAncestorPage(HttpResponse Response ,string ancestorTitle ,string frameName ,string targetUrl ,bool isCloseCurPage)//targetPageTitle 目标页面的title { StringBuilder scriptString = new StringBuilder(); scriptString.Append(" "); Response.Write(scriptString.ToString()); } #endregion3.如何刷新本页面中的其它框架了?RefreshTargetFrameInSamePage#region RefreshTargetFrameInSamePage /**//// /// 从某一框架刷新同一页面中的任意一框架(包括自己所处的框架) /// public static void RefreshTargetFrameInSamePage(HttpResponse Response ,string frameName ,string targetUrl) { string scripStr = string.Format("" ; Response.Write(scripStr) ; } #endregion 4.如何调用祖先页面的脚本?CallAncestorScriptMethod#region CallAncestorScriptMethod /**//// /// 调用祖先页面中的某个框架内部page的脚本 ,如果是调用祖先页面的脚本,targetFrameName传入null /// public static void CallAncestorScriptMethod(HttpResponse Response ,string targetPageTitle ,string targetFrameName ,string methodName ,string[] paraStrs) { StringBuilder scriptString = new StringBuilder(); scriptString.Append(""); scriptString.Append("var p = window ;"); scriptString.Append(string.Format("while(p.document.title != '{0}')" ,targetPageTitle)); scriptString.Append("{"); scriptString.Append("p = p.opener ;"); scriptString.Append("}"); if(targetFrameName != null) { if(paraStrs == null) { scriptString.Append(string.Format("p.frames['{0}'].{1}() ;" ,targetFrameName ,methodName )); } else { string rParaStr = string.Format("'{0}'" ,paraStrs[0]) ; for(int i=1 ;i"); Response.Write(scriptString.ToString()); } #endregion5.如何调用本页面中其它框架page的脚本?CallTargetFrameScriptMethodInSamePage#region CallTargetFrameScriptMethodInSamePage /**//// /// 调用本页面中其它框架内部page的脚本 , /// public static void CallTargetFrameScriptMethodInSamePage(HttpResponse Response ,string targetFrameName ,string methodName ,string[] paraStrs) { StringBuilder scriptString = new StringBuilder(); scriptString.Append(""); if(paraStrs == null) { scriptString.Append(string.Format("window.parent.{0}.{1}() ; ;" ,targetFrameName ,methodName)); } else { string rParaStr = string.Format("'{0}'" ,paraStrs[0]) ; for(int i=1 ;i"); Response.Write(scriptString.ToString()); } #endregion 可见上述这些功能都是通过脚本完成的,如果对脚本不熟悉,是不可能做好Web开发的!