Changing an IFrames InnerHtml from codebehind
Asked Answered
H

5

9

I'm trying to set the HTML of an Iframe at runtime, from code behind.

In my aspx page i have:

<asp:Button ID="btnChange" runat="server" Text="Change iframe content" 
onclick="btnChange_Click" />

<br />

<iframe id="myIframe" runat="server" />

in the code behind:

protected void btnChange_Click(object sender, EventArgs e)
{
    myIframe.InnerHtml = "<h1>Contents Changed</h1>";
}

When i run this.... it posts back, but doesn't change the myIframe contents at all... What am i doing wrong??


I need to do this because im implementing 3D secure into my checkout process.. basically:

1) customer enters credit card details 2) form is submitted, checks with payment gateway if 3d secure is required. if so, url is generated for the banks secure location to enter information 3) i create a POST request to this url, that contains a long security token, and a few other bits of information. i get hold of the HTML returned from this POST request, and need to display it in an iFrame.

Heres what the documentation says to do:

<html>
<head>
<title>Please Authenticate</title>
</head>
<body onload="OnLoadEvent();">
<form name="downloadForm" action="https://mybank.com/vbyv/verify" method="POST">
<input type="hidden" name="PaReq" value="AAABBBBCCCCHHHHHH=">
<input type="hidden" name="TermUrl" value="https:// www. MyWidgits.Com/next.cgi">
<input type="hidden" name="MD" value="200304012012a">
</form>

<script language="Javascript"> <!-- function OnLoadEvent(){ document.downloadForm.target = "ACSframe"; document.downloadForm.submit(); } //--> </script>

<!-- MERCHANT TO FILL IN THEIR OWN BRANDING HERE -->
<iframe src="blank.htm" name="ACSframe" width="390" height="450" frameborder="0">
</iframe>
<!-- MERCHANT TO FILL IN THEIR OWN BRANDING HERE -->
</body>
</html>
Hallett answered 14/8, 2009 at 13:12 Comment(2)
my guess why iframe is chosen, is because the post response contains html head and body tagsAngelineangelique
probably a better way than doing nothing is to extract the necessary information from the post response rather than dumping whole as contentAngelineangelique
H
14

You can try this:

protected void btnChange_Click(object sender, EventArgs e)
{
   myIframe.Attributes["src"] = "pathtofilewith.html"
}

or maybe this will work too:

protected void btnChange_Click(object sender, EventArgs e)
{
   myIframe.Attributes["innerHTML"] = "htmlgoeshere"
}
Hickman answered 14/8, 2009 at 13:43 Comment(0)
P
3

There's no innerHTML attribute for an iFrame. However, since HTML 5.0, there's a new srcdoc attribute. http://www.w3schools.com/tags/tag_iframe.asp

Value: HTML_code

Description: Specifies the HTML content of the page to show in the < iframe >

Which you could use like this:

protected void btnChange_Click(object sender, EventArgs e)
{
    myIframe.Attributes["srcdoc"] = "<h1>Contents Changed</h1>";
}
Pantechnicon answered 3/6, 2014 at 18:14 Comment(0)
C
0

You can not change iframe innerHTML property. It does not have innerHTML property at all. Try to RegisterStartupScript and use document.write to change the content of the iframe since it is a window.

By the way, i think HTML tag is better place for this.

Cordell answered 14/8, 2009 at 13:59 Comment(0)
A
0
<asp:Button ID="btnChange" runat="server" Text="Change iframe content" onclick="btnChange_Click" />
<br />
<asp:Literal id="myIframe" runat="server" />

in the code behind:

protected void btnChange_Click(object sender, EventArgs e){
    myIframe.Text = "<h1>Contents Changed</h1>";
}
Angelineangelique answered 14/8, 2009 at 14:8 Comment(0)
N
0

what you need to do is create a seperate aspx page that is empty and that gets the response and loads it in its own body, in other words replace itself, like

mypage.aspx:

<%@ Page contentType="text/html" %>
//... using your namespace that contains the required functionality
<% Response.Write(MyObject.CreateBody()) %>

then place that page inside ur iframe...

<iframe src="mypage.aspx" ... />

simply put, the iframe is a client side window, you cannot reference its body as an object from the server side, it hasnt been loaded yet!

OR... you can open an html file, dump the response then save and close... that file is referenced by your iframe always. use Text stream objects, or filesystemobject or the like...

PS. i haven't tried any of it

Neel answered 28/10, 2009 at 2:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.