Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I access an IFRAME from the codebehind file in ASP.NET?
    text
    copied!<p>I am trying to set attributes for an IFRAME html control from the code-behind aspx.cs file.</p> <p>I came across a <a href="http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx" rel="noreferrer">post</a> that says you can use FindControl to find the non-asp controls using:</p> <p>The aspx file contains:</p> <pre><code>&lt;iframe id="contentPanel1" runat="server" /&gt; </code></pre> <p>and then the code-behind file contains:</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { HtmlControl contentPanel1 = (HtmlControl)this.FindControl("contentPanel1"); if (contentPanel1 != null) contentPanel1.Attributes["src"] = "http://www.stackoverflow.com"; } </code></pre> <p>Except that it's not finding the control, contentPanel1 is null.</p> <hr> <p><strong>Update 1</strong></p> <p>Looking at the rendered html:</p> <pre><code>&lt;iframe id="ctl00_ContentPlaceHolder1_contentPanel1"&gt;&lt;/iframe&gt; </code></pre> <p>i tried changing the code-behind to:</p> <pre><code>HtmlControl contentPanel1 = (HtmlControl)this.FindControl("ctl00_ContentPlaceHolder1_contentPanel1"); if (contentPanel1 != null) contentPanel1.Attributes["src"] = "http://www.clis.com"; </code></pre> <p>But it didn't help.</p> <p>i am using a MasterPage.</p> <hr> <p><strong>Update 2</strong></p> <p>Changing the aspx file to:</p> <pre><code>&lt;iframe id="contentPanel1" name="contentPanel1" runat="server" /&gt; </code></pre> <p>also didn't help</p> <hr> <p><strong>Answer</strong></p> <p>The answer is obvious, and unworthy of even asking the original question. If you have the aspx code:</p> <pre><code>&lt;iframe id="contentPanel1" runat="server" /&gt; </code></pre> <p>and want to access the iframe from the code-behind file, you just access it:</p> <pre><code>this.contentPanel1.Attributes["src"] = "http://www.stackoverflow.com"; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload