<iframe> problem

G

Grzesiek

Hi!
I have created page which contains some menu buttons and <iframe> with
sub-page. Each menu action on the master web site should change content of
iframe (by changing src attribute). But how to detect when user has opened
subpage not in master's <iframe> ? Is it possible to obtain reference to
master's page from sub-page ? When I'm trying to do this using Page.Parent I
get null exception :(

Here's a piece of code
//master.aspx
<%@ Page language="c#" Codebehind="master.aspx.cs" ... %>
....
<form id="Form1" runat="server" method="post">
<iframe id="myIFrame" runat="server" src="subpage.aspx"></iframe>
<asp:Button id="btnStart" runat="server" Text="Button">
</form>
....

//master.aspx.cs
....
private void btnStart_Click(object sender, System.EventArgs e)
{
myIFrame.Attribures["src"] = some_url_address;
}
....

Thx,
Grzesiek
 
J

jasonkester

As far as the Server is concerned, your page and your IFRAME are in
separate browsers. The frame content has no knowledge of its
container, and the container is never posted back and thus cannon even
redraw itself.

This is all solvable through client-side scripting, but it's not
trivial. I wouldn't recommend attempting it except as a last resort.

Basically, this sounds like an architecture issue. The only reason you
would want to use an IFRAME in the first place is if you knew you
wouldn't have to redraw anything else on the page. You're asking how
to redraw the rest of the page every time you flip out the IFRAME
source, which begs the question: why are you using IFRAMEs at all?


Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
G

Grzesiek

Basically, this sounds like an architecture issue. The only reason you
would want to use an IFRAME in the first place is if you knew you
wouldn't have to redraw anything else on the page.
That's right
You're asking how to redraw the rest of the page every time you flip
out the IFRAME source
No, I just want to check whether sub page is being launched in
iframe or in standalone browser window. The reason is that
it is possible to navigate from sub page to master page, which
contains .... yes iframe with subapge, so it could casue page-cycle,
which wouldn't look nice ;)
which begs the question: why are you using IFRAMEs at all?
It isn't my idea:( I have to have work area and menu. The menu
shouldn't be changed. So because asp .net hasn't got support
for multiple frames I decided to use iframe. Another solution
is to use framesets, bu this requires heavy client-side scripting,
and I'm not advanced in this.

thanks,
Grzesiek
 
J

John MacIntyre

Grzesiek said:
Hi!
I have created page which contains some menu buttons and <iframe> with
sub-page. Each menu action on the master web site should change content of
iframe (by changing src attribute). But how to detect when user has opened
subpage not in master's <iframe> ? Is it possible to obtain reference to
master's page from sub-page ? When I'm trying to do this using Page.Parent
I
get null exception :(
[snip]

Hi Grzesiek,

As far as I know, this needs to be done in javascript.

If you look on the MSDN site, they keep every page within the MSDN frameset.
So if you enter a url like :
http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/top_1.asp
It will redirect you to
http://msdn.microsoft.com/library/d...p/author/dhtml/reference/properties/top_1.asp
If you look at the code on thier page, you will see the code to redirect.
(hint: search for "top == self").

The MSDN script just checks if you opened the page directly, but it doesn't
check if it was opened within an iframe on another site. If you are trying
to avoid this, you should look into window.top.location.href

I've included a small example of what window.top.location.href returns.

Good luck.
--
Regards,
John MacIntyre
http://www.johnmacintyre.ca
Specializing in; Database, Web-Applications, and Windows Software


The parent page with an iframe
-- p.htm --
<html>
<body>
<iframe src="c.htm" frameborder="yes"></iframe>
</body>
</html>
-------

The child page
-- c.htm --
<html>
<head>
<script language="javascript">
function body_onLoad()
{
lbl.innerText=window.top.location.href;
}
</script>
</head>
<body onload="body_onLoad();">
<label id="lbl">Not Set</label>
</body>
</html>
 
J

jasonkester

Ah. Then the Top frame never has to come into play at all. Just throw
a script into every page that needs to live on top that sniffs to
ensure that it is not inside a frame:

<script>
if (self != top)
{
top.location.href = location.href;
}
</script>

If a page with the above script ever finds itself inside an IFRAME or
FRAME, it will reload itself in the top position. That's the standard
fix for this issue.

Good luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top