tags:

views:

960

answers:

3

I want to display an iframe in a .aspx page, and the iframes source should be the same page.

I need to use a relative uri.

What value should I give the 'src' attribute?

I realise this is a little unusual - the page will be displayed in different states depending on parameters passed in, so the iframe won't be displayed within itself.

+2  A: 

The literal relative path should work. IE: MyPage.aspx

Here is an ASP.NET Example...

Seemed to work fine for me with the following...

Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <iframe runat="server" id="myFrame" src="Default.aspx?message=Hello%20World"></iframe>
    <div id="myDiv" runat="server"></div>
    </div>
    </form>
</body>
</html>

Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {
     protected void Page_Load(object sender, EventArgs e)
     {
      string message = Request.QueryString["message"];
      if (null != message)
      {
       myDiv.InnerText = message;
       myFrame.Visible = false;
      }
      else
      {
       myDiv.Visible = false;
      }
     }
    }
}
Quintin Robinson
I tried that - for some reason that doesn't seem to work, the iframe stays empty.
Sophia
Hmm that is odd, it is relative to where you are, are you sure you aren't putting like /MyPage.aspx?.. as an example try creating a folder with 2 html files: one with an Iframe named main.html set the iframe source to sub.html and have the sub.html body say subpage. The example should work.
Quintin Robinson
I guess I should ask too, Filename.aspx is the literal relative path to the currently executing page? It isn't in a subfolder or alternate directory path?
Quintin Robinson
I apologize apparently I missed where you said that you want the iframe source to be the parent page?? Are you injecting the iframe dynamically, because indeed as Thor says it will repeat forever and probably crash a users browser, what is the intent for this?
Quintin Robinson
It is a bit weird, basically I need to keep code for the page as a single file (no code behinds or secondary .aspx pages), and display a PDF inline, in an iframe.So, I thought a solution might be to give the page different modes depending on parameters passed in. It might not be possible :/
Sophia
Oh yeah that is fine as long as the IFrame isn't always rendered out to the server, however as in the example I provided the relative path should still work, let me make sure it will work when referencing the same page.
Quintin Robinson
I added an example the displays the behavior that you want, hopefully that helps clear things up. Let me know if you have any further issues/questions as of right now i'm going to sleep!
Quintin Robinson
Thank you for your help :) Goodnight
Sophia
+3  A: 

If you do this you will get an endless loop... the processsing will "never end". maybe thats why it is white? it is really processing pages.. - is that what you want ? if you for example want just 2-3 pages in depth, you can youse querystring and for example disable the iframe when the querystrings are incremented to 3. MyPage.aspx?depth=1 --MyPage.aspx?depth=2 --MyPage.aspx?depth=3 etc

ThorHalvor
Actually, what he is getting is infinite recursion. Therefore, he should set up a limiting condition and be able to test for that in his server-side processing -- what you said, only better.
dar7yl
+1  A: 

The short answer is src="localfilename.aspx" within the iframe tag. The web standard, loosely applied, says anything not proceeded by a '/' is relative to the location of the current page. Sometimes src="" might even work for substituting the current file name (at the browser level)

dar7yl