Solved

How to tell if ASP.NET app is running inside a frame; using c#, not javascript

Posted on 2009-06-30
9
912 Views
Last Modified: 2013-11-07
I need to be able to determine if my c# ASP.Net application is inside a HTML frame.  In Javascript, I would normally use:
if (top.location != location)

How can I do this in the code-behind of a user control?
0
Comment
Question by:alien_technology
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 3

Expert Comment

by:eirikurh
ID: 24749938
If your mainpage have a default.aspx as your mainpage, and your frame have another url like "main.aspx"
then you can check the url on the server side:
if (Request.Url.Segments.Length > 2)
{
  if (Request.Url.Segments[2].ToLower(CultureInfo.InvariantCulture) != "default.aspx")
  {
     //in Frame
  }
}
0
 

Author Comment

by:alien_technology
ID: 24750080
Thanks for the reply eirikurh, but I don't think that it does what I need.  

It appears that in your example, if main.aspx is loaded by itself, Request.Url.Segments[2] will be "main.aspx".  If main.aspx is loaded inside an iframe, Request.Url.Segments[2] will still be "main.aspx", won't it?  The page main.aspx still does not know that it was loaded inside a frame.
0
 
LVL 3

Expert Comment

by:eirikurh
ID: 24750430
See Request Referrer points to default.aspx when main.aspx is loaded in frame.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:alien_technology
ID: 24754937
But if a link in main.aspx does a postback or links to another page and then back, referrer would no longer point to default.aspx.  

Is there no .Net equivalent to Javascript's top.location?
0
 
LVL 3

Expert Comment

by:eirikurh
ID: 24757235
Do you have control over the frame page.

If i were you, i would add a parameter to the frame.

src="main.aspx?inframe=1
0
 

Author Comment

by:alien_technology
ID: 24757457
I could, but that would mean that the querystring would have to be updated for all links on all pages in the frame.  

Is there no way for a .Net page to access the parent frame to get the URL?  That would definitively indicate if it is in a frame.
0
 
LVL 3

Expert Comment

by:eirikurh
ID: 24759322
The server have no access to the Browsers DOM
Why do you need to check if the page is loaded in frame?
0
 

Author Comment

by:alien_technology
ID: 24798588
We are migrating applications that used to be stand-alone to now work in an iframe in DotNetNuke.  With a single code base, we want to display the header images and menuing if the app is on its own, but remove it if its in a DNN window since DNN can display that stuff.
0
 
LVL 3

Accepted Solution

by:
eirikurh earned 500 total points
ID: 24809963
You could make a cookie on the client side
and read it later on server side

---Client side ---
var seconds = 60;
var date = new Date();
date.setTime(date.getTime() + (seconds*1000)); //1 minute expire
var expires = "; expires=" + date.toGMTString();
var framevalue = '0';
if (top.location != location)
{
  framevalue = '1';  
}
document.cookie = 'inframe'+"="+framevalue+expires+"; path=/";


--- Server Side ---
bool isFrame = false;
if (Response.Cookies["inframe"] != null)
{
  string cookieValue = Response.Cookies["inframe"].Value;
  if (cookieValue.Equals("1"))
    isFrame = true;
}
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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