Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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>

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top