How to dynamically set an IFrame source?

I have an IFrame embedding a youtube video. I want to create a text box where the user (admins) can insert a new src (URL) video, and IFrame a new source. Here is what I still have:

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 there is an 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 do not set any src at the beginning, but when I paste the URL into the text box and click the button, Iframe does not display anything.

+4
source share
2 answers

You need to do this in the client browser, and not on the 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 out of my head though, so don’t have to dump me if it doesn't work for the first time!

+2
source

Other answers do not answer the question; they provide an alternative. The question is how to install iFrame src from C #. I will answer it here.

I’m all for the “right tools for the job” and I use this mantra myself, but only when the other tools are “wrong”. It has not been installed here. Can anyone give a good technical reason why this should not be done in code?

I think the @Pepys problem could be due to something in the url that it hasn't provided yet. For example, perhaps its URL includes ampersands or other characters that need to be escaped.

The following code works great 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"; 
+5
source

Source: https://habr.com/ru/post/1402431/


All Articles