Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp:LinkButton doesn't work from inside an iframe

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.

like image 475
Patrick Szalapski Avatar asked Feb 22 '26 20:02

Patrick Szalapski


2 Answers

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.

like image 100
gbs Avatar answered Feb 25 '26 09:02

gbs


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.

like image 32
Shashank Chaturvedi Avatar answered Feb 25 '26 10:02

Shashank Chaturvedi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!