• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

飞天牛

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

Asp.net实用技巧(2)

1. 在Asp.net实用技巧(1) 中提到了如何刷新父页面,那么如果要刷新父页面的父页面的父页面了?那就是刷新祖先页面RefreshAncestorPage。
#region RefreshAncestorPage
        
/**//// <summary>
        
/// 刷新指定的祖先页面,注意是"祖先页面"    
        
/// </summary>        
        public static void RefreshAncestorPage(HttpResponse Response ,string targetPageTitle ,
bool isCloseCurPage)//targetPageTitle 目标页面的title
        {            
            StringBuilder scriptString 
= new StringBuilder();
            scriptString.Append(
"<script language = javascript>");
            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(
"</"+"script>");

            Response.Write(scriptString.ToString());
        }

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

2.如何刷新祖先页面中的某个frame中的page了?
#region RefreshFrameInAncestorPage
        
/**//// <summary>
        
/// 刷新指定的祖先页面中的某个框架的内部页面
        
/// </summary>        
        public static void RefreshFrameInAncestorPage(HttpResponse Response ,string ancestorTitle ,
string
 frameName ,string targetUrl ,bool isCloseCurPage)//targetPageTitle 目标页面的title
        {            
            StringBuilder scriptString 
= new StringBuilder();
            scriptString.Append(
"<script language = javascript>");
            scriptString.Append(
"var p = window ;");
            scriptString.Append(
string.Format("while(p.document.title != '{0}')" ,ancestorTitle));
            scriptString.Append(
"{");            
            scriptString.Append(
"p = p.opener ;");
            scriptString.Append(
"}");            
            scriptString.Append(
"p.focus();");
            scriptString.Append(
string.Format("p.{0}.location = '{1}';" ,frameName, targetUrl));

            
if (isCloseCurPage )
            {
                scriptString.Append( 
" window.focus();" );
                scriptString.Append( 
" window.opener=null;" );
                scriptString.Append( 
" window.close(); " );
            }            

            scriptString.Append(
"</"+"script>");

            Response.Write(scriptString.ToString());
        }        
        
#endregion
3.如何刷新本页面中的其它框架了?
#region RefreshTargetFrameInSamePage
        
/**//// <summary>
        
/// 从某一框架刷新同一页面中的任意一框架(包括自己所处的框架)
        
/// </summary>        
        public static void RefreshTargetFrameInSamePage(HttpResponse Response ,string frameName ,
string
 targetUrl)
        {                
            
string scripStr = string.Format("<script language ='javascript'> window.parent.{0}.location= '" ,
frameName) 
+targetUrl + "'";
            scripStr 
+= "</"+"script>" ;            
            Response.Write(scripStr) ;
        }
        
#endregion 
4.如何调用祖先页面的脚本?
#region CallAncestorScriptMethod
        
/**//// <summary>
        
/// 调用祖先页面中的某个框架内部page的脚本 ,如果是调用祖先页面的脚本,targetFrameName传入null
        
/// </summary>        
        public static void CallAncestorScriptMethod(HttpResponse Response ,string targetPageTitle ,
string
 targetFrameName ,string methodName ,string[] paraStrs)
        {
            StringBuilder scriptString 
= new StringBuilder();
            scriptString.Append(
"<script language = javascript>");
            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<paraStrs.Length ;i++)
                    {
                        rParaStr 
+= string.Format(", '{0}'" ,paraStrs[i]) ;
                    }
                    scriptString.Append(
string.Format("p.frames['{0}'].{1}({2}) ;" ,targetFrameName ,methodName ,
rParaStr));    
                }
            }
            
else
            {
                
if(paraStrs == null)
                {
                    scriptString.Append(
string.Format("p.{0}() ;" ,methodName ));    
                }
                
else
                {
                    
string rParaStr = string.Format("'{0}'" ,paraStrs[0]) ;
                    
for(int i=1 ;i<paraStrs.Length ;i++)
                    {
                        rParaStr 
+= string.Format(", '{0}'" ,paraStrs[i]) ;
                    }
                    scriptString.Append(
string.Format("p.{0}({1}) ;" ,methodName ,rParaStr));        
                }
                
            }            

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

5.如何调用本页面中其它框架page的脚本?

#region CallTargetFrameScriptMethodInSamePage
        
/**//// <summary>
        
/// 调用本页面中其它框架内部page的脚本 ,
        
/// </summary>        
        public static void CallTargetFrameScriptMethodInSamePage(HttpResponse Response ,
string
 targetFrameName ,string methodName ,string[] paraStrs)
        {
            StringBuilder scriptString 
= new StringBuilder();
            scriptString.Append(
"<script language = javascript>");

            
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<paraStrs.Length ;i++)
                {
                    rParaStr 
+= string.Format(", '{0}'" ,paraStrs[i]) ;
                }
                scriptString.Append(
string.Format("window.parent.{0}.{1}({2}) ; ;" ,targetFrameName ,methodName,
rParaStr));    
            }
                
            scriptString.Append(
"</"+"script>");
            Response.Write(scriptString.ToString());
        }
        
#endregion

 

posted on 2006-03-25 11:24  飞天牛  阅读(176)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3