Need parent name of Iframe - asp.net

I've been searching for a couple hours and haven't found a solution. Hopefully you can help.
I want to get the name & path of the parent page within an Iframe. The reason for this is to prevent the page within the Iframe to be displayed outside it's intended location. If I can get the session variables of the parent page, that would be even better as I could verify the user.
The Iframe will be written in ASP.NET, while the parent page is PHP.
I have found Javascript code that might work, but I'm very unfamiliar with javascript and how to pass the return value to the code behind in ASP.NET. If your solution involves javascript, I will need the whole thing from calling it in the form load to getting back the value.
I have tried these functions, but they don't show what I need.
Request.Url.AbsoluteUri.ToString
Request.ApplicationPath.ToString
Request.UrlReferrer.AbsolutePath.ToString
Thanks

I understand that you looking for a code behind solution and redirect, but the code behind can not recognize if the page is calling from some other.
You may try the Request.ServerVariables["HTTP_REFERER"] but this can make mistake, and can even been disable by some users, or changed.
One simple working solution with javascript is to check on the iframe it self if its inside your master page or not. So this code go to the page that lives inside the iframe, and if see that is alone is redirect the user back to the master page.
<script type="text/javascript" language="javascript">
// this line is check if its inside an iframe.
// if top==self then is not in iframe
if(top == self)
{
alert("This page can not be viewed alone, please press ok to load the master page");
top.location.replace("MasterPage.aspx");
}
</script>

Related

Finding master page control from within iFrame

Scenario:
I have one main master page say MasterPage1. In that master page I have a splitter. In that splitter there is an iframe. Within that iframe we load another master page say "MasterPage2". In MasterPage2 we load a page on which different User Controls are rendered.
Problem:
Now I want to find a control on MasterPage1 from my User Control loaded on the page in MasterPage2.
Please help....
Problem To your Scenario:
masterpages and content pages are rendered as a single object, thus the page class is able to reference every element found in both the objects(master and content page). When you are rendering an iframe the iframe content is requested by client hence no reference exists. so it is not possible to reference each other on server.
Solution to the problem
From above you must have realized all the problem is the reference , so you will have to hack inti it. the simplest way I can think is to use querystring.
call the iframe page with querystring containing a identifier to the masterpage like mpage=mpage1,mpage=mpage2 etc.
Now in masterpage2 request the querystring to find which masterpage is applied and proceed. This way you will have little relaxaction because masterpage1 content cannot be changed but masterpage2 can be.
Now you will need to work more to what you need. Proceed only if this is the only way to solve the real problem(I think the problem is not masterpage but the solution to the problem that is making you to do these weired things).
Well for that you will have to use javascript and handlers which will render and return the rendered usercontrol. But i seriously say not to use this setup in production and find other alternative by changing your code to use usercontrol instead of iframe.

Detect Click On Iframe Then Do An Action

I want to add a iframe to my website that has a button in it and when someone clicks on it, it does an action like maybe forward to another page or echo a message. I don't care about the code whether its java, php, html, etc as long as it works. I'm good at coding certain things but this is not one of them, help me out please.
UPDATE:
the iframe is:
iframe src="http://ipget.tk" /iframe
when its clicked on I want it to go to google.com
the iframe will be on a test page on scazioco.com
Using Jquery you can bind an event to your IFrame :
$('#iframe_id').bind('click', function(event) {
window.location = http://www.google.com
});
Replace #iframe_id with the actual Iframe's ID

iframe security

I ve a task in which I insert on a page iframe only when user enters valid code. User enters a code I check whether its valid and if it is I insert iframe element on my site. It works great but I wonder if its possible to hide src of the iframe cause when one knows the source of the iframe can get onto this site whenever one want without valid code any more.
is it common problem ? what can I do about this ?
You can not hide the source. Find another way to do it.
You can't hide the src of an iframe. Even if you could hide it from the user, it's easy enough to find, after all, your browser has to make an HTTP request. It seems the easiest solution to your problem is just have something like:
if (code == whateverTheCodeIsSupposedToBe){
Session["CorrectCode"] = true;
}
Then in your iframe page do:
if (Session["CorrectCode"] != true){
Response.Redirect("SorryYouDontHaveTheRightCode.aspx");
}
As the others have said, the browser NEEDS the iframe source to draw it, so there really isn't a way to tell the browser the src and at the same time NOT tell it.
I'll try to add some value here with respect to your underlying problem. Why not pass the validation code that the user enters to the page inside the frame and have that page verify it is correct?
Then it doesn't matter if the user knows the src of the frame, without the validation code it won't do them any good.

How to react in ASP.NET code-behind class when a hyperlink is clicked inside the page's IFrame?

I have an ASP.Net page containing an IFrame. In the IFrame I load a html document. When the user clicks on a hyperlink in the content of the IFrame, I would need a callback to be called in the code-behind class of the ASP.Net page.
I guess that I need Ajax to do this but I'm not exactly sure about what I need to do. Could you give me some pointers?
By the way I'm fairly new to ASP.Net.
Thanks
A lot of this depends on what it is you want to do specifically.
The problem you've got is that the DOM of the page in the iframe doesn't appear to be in the DOM of the calling page. All the calling page sees is the iframe tag as a closed tag, like an image tag. Some browsers will detect a click inside an iframe nested within a DIV as a click in the div so you have
<DIV id="iframediv">
<Iframe blah...>
</DIV>
and then you might be able to use jQuery or similar to detect a click inside iframediv and do stuff.
The real solution would be to try not to use an iframe as, like I said, even this solution won't necessarily pay off. I can think of at least one scenario where not using an iframe is not an option so I'll leave that be.
Other than that Willem's suggestions also seem to be sound.
Because the html document is not an aspx page it will not be able to trigger any code-behind. If you can change the page in the iframe make it an aspx page and handle the click on a LinkButton like you would do otherwise.
An other option is to change the link in the html page to call a custom aspx page that handles your needs, but that will redirect the html-page to the new aspx page.
Or indeed change the link to call a webservice through javascript (XMLHttpRequest) and let that webservice do what you wanted to do in code-behind.
Finally I ended up writing a Control Extender for the IFrame. The Control Extender gets the links contained in the IFrame via the following Javascript:
var frame = this.get_element();
var links = frame.contentWindow.document.getElementsByTagName("a");
I then simply attach an event handler that reacts to each link's onclick event. The event handler calls back the ASP.Net side via a WCF service.
Not complicated to do once you know the various technologies.

How can I get the Google Mini search results, using the iFrame, to not open in the iFrame

I have an asp.net site using the Google Mini. I have a search box on the masterpage that redirects to a search results page that displays the results in an iFrame. This approach is all taken from the documentation provided by Google for the Mini and seems pretty simple.
What the doc doesn't cover (or I can't find) is that clicking on any of the result that might be another .aspx page, opens that page with all of it's masterpage glory right inside the iFrame which is obviously not desired. I end up with a page in a page.
Short of grabbing the xml search results and manipulating that myself, how do I just get the search result links to open like a normal page?
You can add this javascript into your masterpage to make it break out of the iframe when the user clicks the link
<script type="text/javascript">
try
{
if (self.parent.frames.length != 0)
self.parent.location=document.location;
}
catch (Exception) {}
</script>
Another way of doing this would be to use the target attribute of the anchor (<a>) tag. If I remember correctly that would be
Link
I've used the javascript solution myself before and it works, I've not tested the target.

Resources