We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


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

Medium Priority
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?
Watch Question

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


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.

See Request Referrer points to default.aspx when main.aspx is loaded in frame.


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?

Do you have control over the frame page.

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



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.

The server have no access to the Browsers DOM
Why do you need to check if the page is loaded in frame?


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.
Unlock this solution and get a sample of our free trial.
(No credit card required)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.