我有一个嵌入了 YouTube 视频的 IFrame。我想创建一个文本框,用户(管理员)可以在其中粘贴新的视频源代码(URL),而 IFrame 则采用新的源代码。这是我目前所拥有的:
protected void Edited_Click(object sender, EventArgs e)
{
// HtmlControl frame1 = (HtmlControl)this.FindControl("frame1");
string url = TextBox1.Text;
frame1.Attributes["src"] = url;
}
在 html 代码中是 Iframe:
<div id="video">
<iframe title="YouTube video player" runat="server" width="420"
frameborder="1" style="height: 265px; float: left;
text-align: center;" id="frame1"
name="frame1" align="middle"></iframe>
<br />
</div>
我一开始没有设置任何 src,但是当我在文本框中粘贴 URL 并点击按钮时,Iframe 没有显示任何内容。
最佳答案
其他回答没有回答问题,它们提供了替代方案。问题是如何从 C# 设置 iFrame src。我会在这里回答。
我完全赞成“工作需要正确的工具”,并且我自己经常使用这个口头禅——但只有在其他工具“错误”的时候。这在这里还没有成立。有人可以提供一个很好的技术理由为什么不应该在代码隐藏中完成吗?
我认为@Pepys 遇到的问题可能是由于 URL 中的某些内容,他尚未提供。例如,他的 URL 可能包含需要转义的符号或其他字符。
下面的代码适合我:
excelframe.Attributes["src"] =
@"https://r.office.microsoft.com/r/rlidExcelEmbed?"
+ @"su=-0000000000"
+ @"&Fi=zzzzzzzzzzzz!111"
+ @"&ak=x%3d9%26x%3d9%26x%3d!zzzzzzzzzz"
+ @"&kip=1"
+ @"&AllowTyping=True"
+ @"&ActiveCell='sheet1'!C3"
+ @"&wdHideGridlines=True"
+ @"&wdHideHeaders=True"
+ @"&wdDownloadButton=True";
关于c# - 如何动态设置 IFrame 的源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9787250/