Change the Src of an iFrame from another Page

Posted on 2006-05-07
Last Modified: 2007-12-19
Hi All,
    I wonder if what I want to do is possible. On My main page I create TWO iFrames Side by Side based on a Session Variable:

Response.Write("<iframe scrolling=no width=256 height=1024 frameborder=no bordercolor=silver id='iframemenu' src='" + Session["PageMenu"].ToString() + "'></iframe>");

Response.Write("<iframe scrolling=no width=256 height=1024 frameborder=no bordercolor=silver id='iframetarget' src='" + Session["PageURL"].ToString() + "'></iframe>");

  So there are two pages side by side, one is a menu and the other is the target for the menu items.

 I am using an ASP:Menu in the Menu page and in the the Callback

 protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)

 I want to change the src of the OTHER iframe  

 PSEUDOCODE  iframe["iframetarget"].src = "xcxxx"

If I can't do that I simply want to refresh the Main page that creates BOTH the iframes that way I can repopulate the two ifrrames using session variables.

 PSEDOCODE    thepagewhocreatedme.Refresh  or  URLThatWasTypedintpTheBrwwser.Refresh or RefreshBrowserPages

 Spent waay toooo long trying to find out how to do this



Question by:williamcampbell
    LVL 1

    Expert Comment

    Are you saying that you want to change the src of one iFrame from within the other iFrame? If so, then yes you can do this. I have done it before. If I remember correctly you need to reference the parent, and then the parent (you call it Main page) is able to change the source of the other iFrame. Something like, parent.iFrame2.src = 'whateverSrc.aspx'. One thing, I think I did that with Javascript, so you might need to call a Javascript function to change the src of the other frame, at least with the way I did it.
    LVL 12

    Author Comment

    FP Yes I am trying to update the iFrames src from another iFrame.

      I think your suggestion is correct however I don't know how to:

     1) Get the Parent Page in C#
     2) Call a Javascript Function from C#

      You said you had done this before..can you dig up some source code :)

    LVL 1

    Accepted Solution

    Let me look around, I'll post in a little while....
    LVL 1

    Expert Comment

    Here's an example:

    You have 'Main.html' and the two iFrame pages, 'iFrame1.html' & 'iFrame2.html'

    1) Main.html: load the iFrames like usual


    <frameset cols="50%,50%">
    <frame src="iFrame1.html" name="iFrame1">
    <frame src="iFrame2.html" name="iFrame2">


    2) iFrame1: Your navigation page

    You will need to include the following Javascript in the <head> of the navigation page:

    <script language="JavaScript"><!--

    Function changeFrame2Src(location) {
    this.parent.iFrame2.src = location; }


    3) Your menu control will look something like the following:

      <asp:Menu runat="server">
    <asp:MenuItem navigateUrl="javascript:changeFrame2Src(iFrame3.html)">Change target to iFrame3</asp:MenuItem>
    <asp:MenuItem navigateUrl="javascript:changeFrame2Src(iFrame4.html)">Change target to iFrame4</asp:MenuItem>

    *Keep in mind that I haven't tested the code!*

    Another way to do it is to put the changeiFrameSrc function in the <head> of Main.html and call 'parent.changeiFrameSrc()'. You would then have to change the line of the function to: 'iFrame2.src = location;'

    Hope this works for you!

    (I think they are talking about your same problem here:
    LVL 12

    Author Comment



       Well I tried your suggestion and various incantations of it to no avail.

    // On the main page
    Response.Write("<iframe scrolling=no width=100% height=1024 frameborder=no bordercolor=silver id='iframetarget' src='" + Session["PageURL"].ToString() + "'></iframe>");

    I'm not using a frameset I am dynamicaly creating the iframe and as far as i can ascertain there is no way to access the iframe from another child (
    this.parent.frames["iframetarget"].src = null. Your link suggests calling a function in the parent that can access the iframe that may work.

    However I reverted to my plan B refreshing the parent form and passing a session variable back. This way I can handle things from code behind which is what I really want. After much hair pulling....

        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
            this.Session["PageURL"] = e.Item.PopOutImageUrl.ToString();  // I store the src url in this parameter so sue me ;)
            string l_script = "";
            l_script = "@SCRIPT>parent.document.forms['FrmMain'].submit();self.close()@/SCRIPT>";
            Page.RegisterClientScriptBlock("CloseChild", l_script.Replace("@", "<"));
            // By registering this script the parent page is refreshed and this page closes (Assuming you main page form is frmmain)
            // the parent picks up the session variable and dynamically creates the iframe on refresh

    I hope others find this useful.


    LVL 12

    Author Comment

    Fp passed on da points to you for the help




    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now