How do I dynamically set source of an IFrame?

爷,独闯天下 提交于 2019-12-01 08:12:09

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!

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.

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.

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";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!