3

Suppose that I have an ASP.NET page that is intended for other sites to render inside of their <iframe>. Suppose further that my page includes some server-side <asp:LinkButton> controls, which get rendered as <a href="javascript:__doPostBack(...)"> links.

I would be happy if _doPostBack() would execute within the iframe, but it does not--Instead I get "_doPostBack not defined" error in the console. I presume that is because it is trying to execute it on the outside page, because it is an href="javascript:..." link and not an ordinary DOM event.

How can I overcome this without changing away from a LinkButton? Is there a better way to run the server-side code I need for clicking the button?

EDIT: I added an the <a href="javascript:alert(document.title)">test</a> and clicked on it, and I got the title of my outer page, not the title of my page in the iframe.

Patrick Szalapski
  • 8,738
  • 11
  • 67
  • 129
  • 2
    Your postback will in no way interact with the parent window only within the iframe itself. If you want to pass information from the iframe to the parent you will likely have to dive into javascript to achieve that. – Caimen Jan 24 '14 at 00:08
  • 2
    Are the pages on the same domain? If so, it should be possible with a little Javascript. If not, there is absolutely no way to do this using JS as it violates the [same origin policy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript) – Basic Jan 24 '14 at 00:14
  • The pages are not on the same domain, but I would be happy to execute the __doPostBack() within the iframe. Instead I get "__doPostBack not defined" error in the console. (I have confirmed that __doPostBack is defined in the iframe but not on the outer page.) I presume that is because it is trying to execute it on the outside page, because it is an href="javascript:..." link and not an ordinary DOM event. Do I understand that right? – Patrick Szalapski Jan 24 '14 at 02:56
  • Write some HTML that has a href of `javascript:alert(document.title)`, that will tell you what document context it is running in (assuming your iFrame has a different title). – Timothy Walters Jan 24 '14 at 03:16
  • Good idea! Confirmed that it is giving me the outer page's title, not the page in the iframe. – Patrick Szalapski Jan 24 '14 at 18:12
  • I just tried to replicate it in JSfiddle and I could not. Everything works fine. http://jsfiddle.net/szal/9qWMr/6/embedded/result/ . Why would my actual ASP.NET page give me the outer page title, but this JSFiddle give the inner iFramed page title? – Patrick Szalapski Jan 24 '14 at 19:23
  • Thank you all for your help. It turns out I had a target="_top" on the link that I didn't notice until much later. Sorry for the wild goose chase! I'll give the credit to the answer that led me to figure it out. – Patrick Szalapski Jan 30 '14 at 14:24

4 Answers4

1

I might not be getting the question right...but the link button in your iframe should work fine only inside the iframe context. So anything in the code-behind of the iframe .aspx will work as regular.

Now if you want the link_button in iframe to interact with the parent page the nope. What you are trying to do is a Cross-domain scenario. I doubt there's any way you might be able to accomplish it and that's due to security reasons. I was looking for something like that as asked here.

If you own both the websites then you might want to check if WebService is of any use.

Community
  • 1
  • 1
gbs
  • 7,196
  • 5
  • 43
  • 69
  • I would be happy if the _doPostBack would execute inside the iframe, but it does not. Instead I get "__doPostBack not defined" error in the console. I presume that is because it is trying to execute it on the outside page, because it is an href="javascript:..." link and not an ordinary DOM event. Do I understand that right? (I do not own the outer web site.) – Patrick Szalapski Jan 24 '14 at 02:54
  • Ahh...are you experiencing that IE10 & IE11 bug. http://stackoverflow.com/questions/18244223/webform-dopostbackwithoptions-is-undefined-in-ie11-preview – gbs Jan 24 '14 at 17:45
  • No, testing in Chrom and Firefox "but the link button in your iframe should work fine only inside the iframe context"--I don't think that is so, because it is a link, which only exectutes in the topmost page. – Patrick Szalapski Jan 24 '14 at 18:35
  • Hmm, I just tried this in Jsfiddle and everything works...there must be something unique about ASP.NET webforms that is defeating the ordinary behavior. http://jsfiddle.net/szal/9qWMr/6/embedded/result/ – Patrick Szalapski Jan 24 '14 at 19:21
  • Are you trying something like this: http://msonweb.azurewebsites.net/iframetest.aspx – gbs Jan 24 '14 at 19:31
  • Yes, that's exactly it. Your example there works great. My real page doesn't. I will look more. – Patrick Szalapski Jan 24 '14 at 19:33
  • Ok that's what I meant by: "...the link button in your iframe should work fine only inside the iframe context." – gbs Jan 24 '14 at 19:38
  • 1
    Thank you all for your help. It turns out I had a target="_top" on the link that I didn't notice until much later. Sorry for the wild goose chase, but you did help me figure it out. – Patrick Szalapski Jan 30 '14 at 14:24
1

I was pretty curious to try it myself, so I created a test application and I was easily able to get a postback within the IFrame.

Now a little clarification if a postback is happening inside an IFrame, you will even see the effects within the frame bounds (unless programmed other wise using some client script). Now to your issue there is no possibility of you invoking the parent __dopostback event, under normal circumstances (but you may like to see this link for a probable thing that might be happening at your parent page).

At times as a security measures the javascript is disabled on individual IFrames (the feature is readily available with HTML5). There is a good possibility that you may be dealing with a similar situation. In that case you do not have any control over the functioning, unless you fall back to simple HTML forms and post it to a different ASP.net page.

Based on our discussion below I would suggest you to use simple HTML hyper link in your page, unless you can change the code of the parent page. If postback is desirable then you can make use of cross page posting (as I already mentioned) and from that page redirect back to your current child page.

One of these codes should work for you:

<a href="#" onclick="YourFormName.submit()">Link Text</a>

OR

<a href="#" onclick="document.getElementById("myForm").submit();">Link Text</a>

OR

<a href="#" onclick="document.getElementById("Iframe Name").contentWindow.document.getElementById("YourFormName".submit)">Link Text</a>

OR

<a href="#" onclick="document.getElementById("Iframe Name").contentDocument.document.getElementById("YourFormName".submit)">Link Text</a>

If none of the above codes works for you then you should consider putting a submit button instead of hyperlink.

Hope this helps.

Community
  • 1
  • 1
Shashank Chaturvedi
  • 2,756
  • 19
  • 29
0

Also curious, I tried to replicate your issue but the post back worked fine inside an iframe. It may sound silly but can you confirm that the page works outside of an iframe?

Other suggestions:

  1. Try different browsers
  2. Disable browser extensions

Try creating a basic HTML page on your local pc with an iframe to your website and see if it works that way. That could tell you if the parent page is the culprit.

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
<title></title>
</head>
<body style="background: #ccc">
    <h1>My HTML Page</h1>
    <iframe src="http://MyWebsiteUrl" width="400" height="400" />
</bod>
</html>
Daniel Kereama
  • 911
  • 9
  • 17
0

Well I had the same problem. Ended up adding asp:imgButton controls with an OnClick="" aspx.cs code behind handler.

The handler their click event called was the one I wanted my linkbutton to call but wouldn't in Safari 7.0.1 on Mac. I put an OnClientClick="return mylinkbuttonHandler()" client side handler in javascript that just invoked my hidden (because they have no image assigned) imgButton's click event; the javascript itself looked like the below:

    <script language="javascript" type="text/javascript">
        // Fake out Safari by creating imgButtons that fire the linkbutton code behind events
        function saveButtonLogic() {
            var buttonId = document.getElementById("hiddenSaveButton");
            buttonId.click();
            return false;
        }
        function submitButtonLogic() {
            var buttonId = document.getElementById("hiddenSubmitButton");
            buttonId.click();
            return false;
        }
        function clearButtonLogic() {
            var buttonId = document.getElementById("hiddenClearButton");
            buttonId.click();
            return false;
        }
    </script>

Hey, it's ugly but it worked great.