I have a page with a very data intensive grid which needs to be
automatically refreshed constantly if a change is detected. In order to
not refresh the complete page so often, I created an iframe on my page
whose html has the refresh meta as follows :
<meta http-equiv="refresh" content="10">
The iframe is effectivelyh refreshed every 10 seconds without having the
grid refreshed. Now, inside the Page_Load of the iframe page I am
determining if the grid needs to be refreshed.
How can I instruct the parent page to refresh itself?
Thanks,
Jeronimo
Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, your question is how to refresh a parent page from a
sub page which is located in a <iframe> tag in the parent page.
If there is anything I misunderstood, please feel free to let me know.
As for this quesiton, I think we can solve it using the client side
javascript. Generally, when we want to retrieve the parent page's object
handle in a sub page( in <iframe> or <frame> ), we can use the
"window.parent" object. If
we want to refresh a page, we can use set the
"window.location.href=window.location.href" to load the page's url again.
So combine the twos, we can use the below javascript code to refresh a
parent page:
"window.parent.location.href = window.parent.location.href;"
Also, since you determine whether to refresh the parent page in the sub
page's serverside Page_Load event handler, you need to add the above
javascript block into the page in Page_Load. You can use the
"Page.RegisterStartupScript" function to register a client script block.
For example:
Page.RegisterStartupScript("RefreshParent","<script
language='javascript'>RefreshParent()</script>");
You can call this when you need to refresh the parent page in the Page_Load
event handler.
In addition, to make the above things clearly, I've made two demo pages,
you may try them out if you like:
------------parent aspx page:-----------------------
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td>
<asp:Label id="lblMessage" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<iframe src="SubPage.aspx"" width="100%" height="1"></iframe>
</td>
</tr>
</table>
</form>
</body>
</HTML>
------------parent code-behind page class------------------
............
private void Page_Load(object sender, System.EventArgs e)
{
lblMessage.Text = System.DateTime.Now.ToLongTimeString();
}
................
-----------------Sub page (in parent page's iframe) aspx page
code----------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>SubPage</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<meta http-equiv="refresh" content="1">
<script language="javascript">
function RefreshParent()
{
window.parent.location.href = window.parent.location.href;
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
</form>
</body>
</HTML>
-------------------Sub page code-behind class
code---------------------------
public class SubPage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
System.Random rnd = new Random();
int i = rnd.Next();
if(i%2 == 0) //simulate determine whether to refresh parent page
{
Page.RegisterStartupScript("RefreshParent","<script
language='javascript'>RefreshParent()</script>");
}
}
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
------------------------------------------------------
Please try out the preceding suggestion. If you have any questions on them,
please feel free to let me know.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Thanks!!! It worked like a charm.
Jeronimo