首页 > 编程 > .NET > 正文

Asp.net实用技巧(2)

2024-07-10 12:58:58
字体:
来源:转载
供稿:网友
 

1. 在asp.net实用技巧(1) 中提到了如何刷新父页面,那么如果要刷新父页面的父页面的父页面了?那就是刷新祖先页面refreshancestorpage。

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了?

refreshframeinancestorpage#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.如何刷新本页面中的其它框架了?

refreshtargetframeinsamepage#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.如何调用祖先页面的脚本?

callancestorscriptmethod#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的脚本?

calltargetframescriptmethodinsamepage#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

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表