C# 从代码隐藏更改 IFrames InnerHtml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1277776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing an IFrames InnerHtml from codebehind
提问by Alex
I'm trying to set the HTML of an Iframe at runtime, from code behind.
我试图在运行时从代码后面设置 iframe 的 HTML。
In my aspx page i have:
在我的 aspx 页面中,我有:
<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??
当我运行它时......它回发,但根本不改变 myIframe 内容......我做错了什么?
I need to do this because im implementing 3D secure into my checkout process.. basically:
我需要这样做,因为我在我的结账过程中实现了 3D 安全......基本上:
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.
1) 客户输入信用卡详细信息 2) 提交表格,如果需要 3d 安全,请与支付网关核对。如果是这样,则为银行安全位置生成 url 以输入信息 3) 我创建一个 POST 请求到此 url,其中包含一个长安全令牌和一些其他信息。我得到了从这个 POST 请求返回的 HTML,需要在 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>
采纳答案by Mr. Smith
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"
}
回答by Emrah GOZCU
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.
您不能更改 iframe innerHTML 属性。它根本没有 innerHTML 属性。尝试 RegisterStartupScript 并使用 document.write 更改 iframe 的内容,因为它是一个窗口。
By the way, i think HTML tag is better place for this.
顺便说一句,我认为 HTML 标签是更好的地方。
回答by mangokun
<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>";
}
回答by Ayyash
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
您需要做的是创建一个单独的 aspx 页面,该页面为空并获取响应并将其加载到自己的正文中,换句话说,替换自身,例如
mypage.aspx:
我的页面.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 中...
<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!
简单地说,iframe 是一个客户端窗口,你不能从服务器端将其主体作为对象引用,它还没有被加载!
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...
或者...您可以打开一个 html 文件,转储响应,然后保存并关闭...您的 iframe 始终引用该文件。使用文本流对象,或文件系统对象等...
PS. i haven't tried any of it
附注。我还没试过
回答by P-L
There's no innerHTML attribute for an iFrame. However, since HTML 5.0, there's a new srcdocattribute. http://www.w3schools.com/tags/tag_iframe.asp
iFrame 没有innerHTML 属性。但是,从 HTML 5.0 开始,有一个新的srcdoc属性。http://www.w3schools.com/tags/tag_iframe.asp
Value: HTML_code
Description: Specifies the HTML content of the page to show in the < iframe >
值:HTML_code
说明:指定页面的 HTML 内容显示在 <iframe> 中
Which you could use like this:
你可以这样使用:
protected void btnChange_Click(object sender, EventArgs e)
{
myIframe.Attributes["srcdoc"] = "<h1>Contents Changed</h1>";
}