Refresh Parent from iframe Page_Load

Discussion in 'ASP .Net' started by Jeronimo Bertran, Jan 17, 2004.

  1. 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
     
    Jeronimo Bertran, Jan 17, 2004
    #1
    1. Advertising

  2. 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.)
     
    Steven Cheng[MSFT], Jan 17, 2004
    #2
    1. Advertising

  3. Hi Steven,

    Thanks!!! It worked like a charm.

    Jeronimo
     
    Jeronimo Bertran, Jan 20, 2004
    #3
  4. Jeronimo Bertran

    Tanveer1909

    Joined:
    Aug 27, 2007
    Messages:
    3
    you can try this too
    parent.frames['Frameid'].window.location.reload();
     
    Tanveer1909, Sep 27, 2010
    #4
  5. Jeronimo Bertran

    aghasoft

    Joined:
    Jul 29, 2011
    Messages:
    2
    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/
     
    aghasoft, Jul 29, 2011
    #5
  6. Jeronimo Bertran

    aghasoft

    Joined:
    Jul 29, 2011
    Messages:
    2
    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>
     
    aghasoft, Jul 29, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sheryl Landon
    Replies:
    4
    Views:
    63,943
    sateswar
    Feb 27, 2012
  2. Scott
    Replies:
    6
    Views:
    1,027
    [MSFT]
    Apr 14, 2004
  3. Bill S.
    Replies:
    2
    Views:
    241
    Bill S.
    May 8, 2004
  4. Replies:
    0
    Views:
    166
  5. PD
    Replies:
    6
    Views:
    756
Loading...

Share This Page