Refresh Parent from iframe Page_Load

J

Jeronimo Bertran

Hi,

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
 
S

Steven Cheng[MSFT]

Hi 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.)
 
Joined
Jul 29, 2011
Messages
2
Reaction score
0
i need same help

hi i am in trouble i need emergency help

i need ifram auto height adjust on menu click event.

i had all done same like hxxp: www. mattcutts. com/blog/iframe-height-scrollbar-example/

that is help i had used.

now that is no page load.

when i use it on click event it did not work first time. and did not restore small height if i call small content page.

this is my link

hxxp: bestforexguide.co.cc/demo/iframtest/
 
Joined
Jul 29, 2011
Messages
2
Reaction score
0
parent page code

<script type="text/javascript">
// Firefox worked fine. Internet Explorer shows scrollbar because of frameborder
function resizeFrame(f) {

f.style.height = f.contentWindow.document.body.scrollHeight + "px";

}
</script>

<body onload="resizeFrame(document.getElementById('childframe'))">


<li onclick="resizeFrame(document.getElementById('childframe'))"><a href="welcome.html" target="childframe" >Welcome</a></li>
<li onclick="resizeFrame(document.getElementById('childframe'))"><a href="weddingevents.html" target="childframe" >Wedding Events</a></li>

<iframe frameborder=0 border=0 src="welcome.html" name="childframe" id="childframe" width="603" style="background-color:#FFF;">
</iframe>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top