Asp.net实用技巧(2)
1. 在Asp.net实用技巧(1) 中提到了如何刷新父页面,那么如果要刷新父页面的父页面的父页面了?那就是刷新祖先页面RefreshAncestorPage。
2.如何刷新祖先页面中的某个frame中的page了?
#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
/**//// <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.如何刷新本页面中的其它框架了?
/**//// <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
#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.如何调用祖先页面的脚本?
/**//// <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
#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
/**//// <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
/**//// <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