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

         可见上述这些功能都是通过脚本完成的,如果对脚本不熟悉,是不可能做好Web开发的!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值