How can I access an IFRAME from the codebehind file in ASP.NET?

I am trying to set attributes for an IFRAME html control from the code-behind aspx.cs file.

I came across a post that says you can use FindControl to find the non-asp controls using:

The aspx file contains:

<iframe id="contentPanel1" runat="server" />

and then the code-behind file contains:
protected void Page_Load(object sender, EventArgs e)
    HtmlControl contentPanel1 = (HtmlControl)this.FindControl("contentPanel1");
    if (contentPanel1 != null)
        contentPanel1.Attributes["src"] = "";


Except that it’s not finding the control, contentPanel1 is null.

Update 1

Looking at the rendered html:

<iframe id="ctl00_ContentPlaceHolder1_contentPanel1"></iframe>

i tried changing the code-behind to:
HtmlControl contentPanel1 = (HtmlControl)this.FindControl("ctl00_ContentPlaceHolder1_contentPanel1");

if (contentPanel1 != null)
    contentPanel1.Attributes["src"] = "";

But it didn’t help.

i am using a MasterPage.

Update 2

Changing the aspx file to:

<iframe id="contentPanel1" name="contentPanel1" runat="server" />

also didn’t help


The answer is obvious, and unworthy of even asking the original question. If you have the aspx code:

<iframe id="contentPanel1" runat="server" />

and want to access the iframe from the code-behind file, you just access it:
this.contentPanel1.Attributes["src"] = "";


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This works for me.


<iframe id="ContentIframe" runat="server"></iframe>

I can access the iframe directly via id.

Code Behind :

ContentIframe.Attributes["src"] = "";

Method 2

If the iframe is directly on the page where the code is running, you should be able to reference it directly:

contentPanel1.Attribute = value;

If not (it’s in a child control, or the MasterPage), you’ll need a good idea of the hierarchy of the page… Or use the brute-force method of writing a recursive version of FindControl().

Method 3

Where is your iframe embedded?

Having this code


<iframe id="iFrame1" runat="server"></iframe>

<form id="form1" runat="server">

      <iframe id="iFrame2" runat="server"></iframe>

I can access with iFrame1.Attributes["src"] just to iFrame1 and not to iFrame2.

Alternatively, you can access to any element in your form with:

FindControl("iFrame2") as System.Web.UI.HtmlControls.HtmlGenericControl

Method 4

Try using



Method 5

Try instantiating contentPanel1 outside the Load event; keep it global to the class.

Method 6

The FindControl method looks in the child controls of the “control” the method is executed on. Try looking through the control collection recursively.

    protected virtual Control FindControlRecursive(Control root, String id)
        if (root.ID == id) { return root; }
        foreach (Control c in root.Controls)
            Control t = FindControlRecursive(c, id);
            if (t != null)
                return t;
        return null;

Method 7

Try this.

ContentPlaceHolder cplHolder = (ContentPlaceHolder)this.CurrentMaster.FindControl(“contentMain”);

HtmlControl cpanel= (HtmlControl)cplHolder.FindControl(“contentPanel1”);

Method 8

<iframe id="yourIframe" clientIDMode="static" runat="server"></iframe>

You should them be able to find your iframe using the findcontrol method.

setting clientIDMode to Static prevents you object from being renamed while rendering.

Method 9

None of your suggestions worked for me, here is my solution:

add src="<%=_frame1%>" //to the iframe id="frame1" html control
public string _frame1 = "";

Method 10

aspx page

<iframe id="fblikes" runat="server"></iframe>

Code behind

this.fblikes.Attributes[“src”] = “/productdetails/fblike.ashx”;

Very simple….

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x