Asp.net实用技巧(1)

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

1 OpenWindowInNewPage #region OpenWindowInNewPage
2 // 在新窗口中打开页面
3 public static void OpenWindowInNewPage(Page curPage , string destUrl)
4 {
5 string scriptString = string .Format( "

 1<script language="JavaScript">window.open('  "  \+  "  {0}  "  \+  "  ','_new');< "  ,destUrl) ;   
 26  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) scriptString  +=  "  /  "  ;   
 37  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) scriptString  +=  "  script> "  ;   
 48  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) if  (  !  curPage.IsStartupScriptRegistered(  "  Startup  "  ))   
 59  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) ![](http://www.cnblogs.com/Images/dot.gif) {   
 610  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) curPage.RegisterStartupScript(  "  Startup  "  , scriptString);   
 711  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif) }    
 812  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif) }    
 913  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif) #endregion 
10
11  
122.如果需要打开固定大小的页面,可以使用如下API 
13
141  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) OpenNewFixSizePage  #region  OpenNewFixSizePage    
152  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) //  打开一个固定大小的页面,如果fullScreen为true ,则high与width不起作用    
163  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) public  static  void  OpenNewFixSizePage(Page page,  string  pageUrl,  bool  isCloseOldPage,  string  scriptName ,  bool  fullScreen ,  int  high ,  int  width)   
174  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) ![](http://www.cnblogs.com/Images/dot.gif) {   
185  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) StringBuilder StrScript  =  new  StringBuilder();   
196  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) StrScript.Append(  "  <script language=javascript> "  );   
207  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) if  (fullScreen)   
218  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) ![](http://www.cnblogs.com/Images/dot.gif) {   
229  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) StrScript.Append(  "  width=screen.Width-10;  "  \+  "  \n  "  );   
2310  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) StrScript.Append(  "  height=screen.height-60;  "  \+  "  \n  "  );   
2411  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif) }    
2512  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) else    
2613  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) ![](http://www.cnblogs.com/Images/dot.gif) {   
2714  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) StrScript.Append(  string  .Format(  "  width={0};  "  ,width)  \+  "  \n  "  );   
2815  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) StrScript.Append(  string  .Format(  "  height={0};  "  ,high)  \+  "  \n  "  );   
2916  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif) }    
3017  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif)   
3118  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 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 +'');  "  );   
3219  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) if  ( isCloseOldPage )   
3320  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) ![](http://www.cnblogs.com/Images/dot.gif) {   
3421  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) StrScript.Append(  "  window.focus();  "  );   
3522  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) StrScript.Append(  "  window.opener=null;  "  );   
3623  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) StrScript.Append(  "  window.close();  "  );   
3724  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif) }    
3825  ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) StrScript.Append(  "  </script>

" );
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( "

 1<script language="javascript"> "  );   
 2![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) scriptString.Append(  "  window.opener.refresh();  "  );   
 3![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) if  (isCloseCurPage )   
 4![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) ![](http://www.cnblogs.com/Images/dot.gif) {   
 5![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) scriptString.Append(  "  window.focus();  "  );   
 6![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) scriptString.Append(  "  window.opener=null;  "  );   
 7![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) scriptString.Append(  "  window.close();  "  );   
 8![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif) }    
 9![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) scriptString.Append(  "  </  "  \+  "  script> "  );   
10![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) Response.Write(scriptString.ToString());   
11![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif) }    
12![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif)   
13![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif) /**/  /*    
14![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) 需要在Father页面的html中添加如下脚本(在Header中):   
15![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) <script language="javascript">   
16![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) function refresh()   
17![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) {   
18![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) this.location = this.location;   
19![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) }   
20![](http://zhuweisky.cnblogs.com/Images/OutliningIndicators/InBlock.gif) </script>

*/
#endregion

以前一直都是做WindowsForm,现在因为公司的项目需要,不得不研究WebForm,以上的几个技巧是这几天解决工作中的问题而总结出来的,如有更好的办法,还请留言告诉我,谢谢!

Published At
Categories with Web编程
Tagged with
comments powered by Disqus