C# 如何动态设置 IFrame 的来源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9787250/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:51:45  来源:igfitidea点击:

How do I dynamically set source of an IFrame?

c#asp.netiframedynamically-generated

提问by Pepys

I have an IFrame embedding a youtube video. I want to create a textbox where user (admins) can paste a new src (URL) of video and the IFrame take the new source. Here is what I have so far:

我有一个嵌入 YouTube 视频的 IFrame。我想创建一个文本框,用户(管理员)可以在其中粘贴视频的新 src(URL),而 IFrame 则采用新的源。这是我到目前为止所拥有的:

protected void Edited_Click(object sender, EventArgs e)
    {
       // HtmlControl frame1 = (HtmlControl)this.FindControl("frame1");
        string url = TextBox1.Text;
        frame1.Attributes["src"] = url;

    }

And in the html code is the Iframe:

在 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>

I don't set any src in the beginning but when I paste a URL in the textbox and hit the button, the Iframe doesn't displays anything.

我一开始没有设置任何 src,但是当我在文本框中粘贴 URL 并点击按钮时,Iframe 不显示任何内容。

采纳答案by DavidWainwright

You need to do this on the client browser, not server-side. I would suggest something like:

您需要在客户端浏览器而不是服务器端执行此操作。我会建议这样的:

// (Add inside script element in head of page html)

window.onload = function() {
    document.getElementById('<id of input>').onchange = function() {
        changeFrameUrl();
    }
};

function changeFrameUrl() {
        var inputVal = document.getElementById('<id of input>').value;
        document.getElementById('<id of iframe>').src = inputVal;
}

Hope this helps - it's off the top of my head though, so don't diss me if it doesn't work first time!

希望这会有所帮助 - 虽然它不在我的脑海中,所以如果它第一次不起作用,请不要贬低我!

回答by TonyG

Other responses don't answer the question, they provide an alternative. The question is how to set iFrame src from C#. I'll answer that here.

其他回答没有回答这个问题,他们提供了一个替代方案。问题是如何从 C# 设置 iFrame src。我会在这里回答。

I'm all for "right tools for the job" and use that mantra a lot myself - but only when the other tools are "wrong". That hasn't been established here. Can someone provide a good technical reason why this should not be done in code-behind?

我完全赞成“适合工作的工具”,并且我自己也经常使用这种口头禅——但只有在其他工具“错误”时才会使用。这还没有在这里成立。有人可以提供一个很好的技术原因,为什么不应该在代码隐藏中完成?

I think the issue @Pepys is experiencing might be due to something in the URL, which he hasn't provided yet. For example, maybe his URL includes ampersands or other characters which need to be escaped.

我认为@Pepys 遇到的问题可能是由于他尚未提供的 URL 中的某些内容。例如,他的 URL 可能包含与号或其他需要转义的字符。

The following code works fine for me:

以下代码对我来说很好用:

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";