Can anybody help me? I am following instructions about how to put an Iframe in my app.

Here is the code for the button:

   protected void Submit_Click(object sender, EventArgs e)
    {
        //frame1 = (HtmlGenericControl)this.FindControl("frame1");
        HtmlControl frame1 = (HtmlControl)this.FindControl("frame1");
        frame1.Attributes["frameborder"] = "0";
        frame1.Attributes["scrolling"] = "auto";
        frame1.Attributes["width"] = 640 + "px";
        frame1.Attributes["height"] = 500 + "px";
        frame1.Attributes["marginheight"] = "0";
        frame1.Attributes["marginwidth"] = "0";   

        frame1.Attributes["src"] = "https://www.google.com/a/speednet.com/LoginAction2?service=mail";

    }

Here is the html:

<iframe id="frame1" scrolling="auto" runat="server">
</iframe>

The problem is pretty simple actually. The line of code:

    HtmlControl frame1 = (HtmlControl)this.FindControl("frame1");

returns a null object for frame1.

So I get the error:

 Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 27:         frame1.Attributes["frameborder"] = "0";

The first line I try to access the Attributes of the frame1 object (which is somehow null after the FindControl didn't find the Control????

Please help

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Check out the info at msdn. You might be better to use a recursive search.
If your control is hosted in a nested naming container then FindControl might not work.
Also, have you tried debugging to check what your iframe's ID is? I've not done a lot of asp.net but i think the naming containers prepend their name to the child controls ID's to ensure they are unique.
So like Label1 placed on Panel1 would have ID = "panel1_Label1".

I tried with your code, and it worked, I've been able to see the innertext of the iframe.

I realised that it wont work, if iframe parent's control are runat="server". Which means

This would work,

<div>
<iframe id="test" runat="server"></iframe>
</div>

but this wouldn't

<div runat="server">
<iframe id="test" runat="server"></iframe>
</div>

Thats why you need a recursive search (Just realised my link wasn't in my post, which might have helped!).
The msdn link shows a method which recursively searches each container looking for the control because the default FindControl only searches the top level. Thats why setting the div to runat server breaks it, because the iframe is no longer at the top level of the server controls.

I realize I was just wasting mine and your time, I found that it is not possible to do what I wanted to do with the Iframe, I was trying to actually set the src to the webmail login but since that login was the gmail interface google (in all its infinite wisdom) has but iframe busting code on its webmail so I guess that messes up the idea of even using an iframe to try to provide a front-end on our site for our users to be embedded in our site with the email client. Wish my boss when he asked me to do this would have known.

Thanks for all your posts to my FindControl problem, but I think guys, I've been on a "wild goose chase"..

Edited 6 Years Ago by lgriess: n/a

Not necesarily :) You might not have used it this tiem, but there will come a time when you need to do something similar in the future and you'll know what to look for.
If just one person learns one thing then it's not a waste of time.

This question has already been answered. Start a new discussion instead.