JS实现各种页面的刷新
JS实现各种页面的刷新功能
1.刷新当前页面
opener.location.replace(opener.location.href);
或者window.opener.window.location.href=window.opener.window.location.href;
2.如果以模态方式在当前页面上打开了一个窗体,在执行完模态窗体的任务后要刷新后台的页面并关闭当前页面,代码如下:
string js = "window.top.opener.window.location.href=window.top.opener.window.location.href;
window.top.close();"或者第一点的刷新代码操作
3.如果以模态方式在当前页面上打开了一个窗体,在执行完模态窗体的任务后要刷新后台的页面并关闭当前页面,同时还要保存后面窗体的一些查询条件进行刷新,那么就需要在当前页面上添加一个隐藏按钮(该按钮实现了查询功能),来实现隐形刷新:
/// <summary>
/// 在客户端弹出消息框(刷新页面)
/// </summary>
/// <param name="page">当前Page</param>
/// <param name="Message">显示消息</param>
/// <param name="refreshOpener">是否刷新父窗口</param>
/// <param name="closeSelf">是否关闭本窗口</param>
/// <param name="refreshLeft">是否刷新左侧菜单</param>
public static void Ajax_Alert(System.Web.UI.Page page, string message, bool refreshOpener, bool closeSelf, bool refreshLeft)
{
StringBuilder strScript = new StringBuilder();
strScript.Append("<script>alert(\"");
strScript.Append(message);
strScript.Append("\");");
if (refreshOpener)
{
strScript.Append("try{var btnRefreshJS=opener.document.getElementById('btnRefreshJS');}catch(err){}");
strScript.Append("if(btnRefreshJS!=null){btnRefreshJS.click();}");
strScript.Append("else{try{ opener.location.replace(opener.location.href);} catch(err){}}");
}
if (refreshLeft)
{
strScript.Append(" try{ opener.parent.fmLeft.document.getElementById('btnRefresh').click();} catch(err){}");
strScript.Append(" try{ parent.fmLeft.location.document.getElementById('btnRefresh').click();;} catch(err){}");
}
if (closeSelf)
{
strScript.Append("parent.window.opener=null;parent.window.close();");
}
strScript.Append("</script>");
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(),
"", strScript.ToString(), false);
}
4.如果本窗体中嵌套了用户控件,用户在用户控件中调用的该刷新js,那么方法如下:
/// <summary>
/// 在客户端弹出消息框(刷新页面)
/// </summary>
/// <param name="page">当前Page</param>
/// <param name="Message">显示消息</param>
/// <param name="refreshOpener">是否刷新父窗口</param>
/// <param name="closeSelf">是否关闭本窗口</param>
/// <param name="refreshLeft">是否刷新左侧菜单</param>
public static void Ajax_UcAlert(System.Web.UI.Page page, string message, bool refreshOpener, bool closeSelf, bool refreshLeft)
{
StringBuilder strScript = new StringBuilder();
strScript.Append("<script>alert(\"");
strScript.Append(message);
strScript.Append("\");");
if (refreshOpener)
{
strScript.Append("try{var btnRefreshJS=parent.window.opener.document.getElementById('btnRefreshJS');}catch(err){}");
strScript.Append("if(btnRefreshJS!=null){btnRefreshJS.click();}");
strScript.Append("else{try{ opener.location.replace(opener.location.href);} catch(err){}}");
}
if (refreshLeft)
{
strScript.Append(" try{ parent.window.opener.parent.fmLeft.document.getElementById('btnRefresh').click();} catch(err){}");
strScript.Append(" try{ parent.window.parent.fmLeft.location.document.getElementById('btnRefresh').click();;} catch(err){}");
}
if (closeSelf)
{
strScript.Append("parent.window.opener=null;parent.window.close();");
}
strScript.Append("</script>");
System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(),
"", strScript.ToString(), false);
}