tags:

views:

4193

answers:

5

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"] = "http://www.stackoverflow.com";

}

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"] = "http://www.clis.com";

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


Answer

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"] = "http://www.stackoverflow.com";
A: 

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

Ian Jacobs
Any suggestions as to where?
Ian Boyd
A little bit of vocab here - Instantiating usually mean actually creating the object instance. What he's doing there is merely getting a reference to an existing object (instantiated by generated code) from a page method.
Jesse Millikan
+1  A: 

Try using

this.Master.FindControl("ContentId").FindControl("controlId")

instead.

Joe R
A: 

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;
    }
RyanFetz
He knows where it is; your solution is a friggen jackhammer where a tack hammer would do.
Will
+1  A: 

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().

AaronSieb
You're right. i just blindly stumbled across it when looking for RyanFetz's recursive find.this.contentPanel1.Attributes["src"] = "http://www.stackoverflow.com";works just fine!i just assumed it wouldn't work because it's not an asp:iframe control. i assume runat=server makes it work?
Ian Boyd
Yeah, using runat=server makes it a server-side control of some kind.
AaronSieb
A: 

Try this.

ContentPlaceHolder cplHolder = (ContentPlaceHolder)this.CurrentMaster.FindControl("contentMain");

HtmlControl cpanel= (HtmlControl)cplHolder.FindControl("contentPanel1");