An ASP.NET Blog
I work for Microsoft and help people and businesses make better use of technolgy to realize their full potential. The opinions mentioned herein are solely mine and do not reflect those of my employer.

Loading pages in IFRAME dynamically from codebehind - ASP.NET

Monday, April 25, 2005 8:03 AM
 
POST UPDATED - March 16, 2007
======================
 

First of all, I thank all of you who have provided feedback and glad to hear that this post has benefited many.  This has been one of my most visited posts and heavily commented one too. 

If you have a query, please write to me using the Contact form.  Sometimes, I miss out your comments and if they are queries in the comments, I might miss out replying to you.  Hence always use Contact form if you have a specific query.

Most of us who develop Web Applications would have used an IFRAME during some stage of our lives. IFRAME's are an easy way by which you can embed another page within your original page such that you can show some important information like Stock position/Weather from another site without worrying about the changes happening to that site and updating the same. The Frame can also be used to show another page from your own application.

ASP.NET also provides the option to have an IFRAME in our ASPX Pages. It can be with/without the "runat=server" attribute and does serve the purpose of embedding the page. The source for the frame can be set as follows:-

 

<IFRAME id="frame1" src="SourcePage.extension / URL of the external Site" scrolling="auto">
</IFRAME>


However, in practical scenarios, we may want to load the page dynamically. In other words, we may want to specify the "src" attribute (the page which we want to show), dynamically. There is no straight forward way to do that and even if you add a "runat=server" attribute to it (though required in the work around provided below), you cannot access the "src" property directly.

The workaround to do that is as follows:-

1. Specify the "runat=server" attribute as follows in the ASPX Page:-

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

2. In the codebehind, you may need to declare a HtmlGenericControl in the control declarations section as follows:-

C#
protected System.Web.UI.HtmlControls.HtmlGenericControl frame1;
 
VB.NET
Protected WithEvents frame1 As System.Web.UI.HtmlControls.HtmlGenericControl 3. Then, you need to do a findcontrol to identify the control on the page and typecast it as follows:-
C#
HtmlControl frame1 = (HtmlControl)this.FindControl("frame1"); 
 
VB.NET
Dim frame1 As HtmlControl = CType(Me.FindControl("frame1"), HtmlControl)

Note: You can have different name for the Generic Control you define in the code behind, but for ease I keep both the same. The "frame1" referred in Find Control is the ID as declared in the ASPX Page.

4. Thereafter, you will be able to access the src property as follows:-

 
C#
frame1.Attributes["src"] = http://www.live.com ;

 
VB.NET
frame1.Attributes("src") = http://www.live.com ;

 
 
NOTE: Thanks PhOeNiX for providing the VB.NET equivalent.  I have added the same now.
As you can see though I have hard-coded the URL you can assign it dynamically based on a condition or any other business logic.

This serves the purpose of dynamically loading the page in the IFRAME.

This method may not be the best method or the only method to achieve this. However, this is a method which worked for me and I wanted to share this for the sake of those who face themselves in this kind of situation.

If there are alternative/better approaches, please feel free to share the same in the Comments section.

Thanks.

Cheers and Happy Programming !!!


Feedback

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Exactly what I was looking for, thanks!!!! 4/29/2005 6:25 AM | Thea Burger

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thank you so much for such a helpful article!! I've been trying forever to figure out how to use a dynamic iFrame, and this works perfectly! 5/3/2005 4:14 PM | Lea McNabb

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hi... .NET fresher here...this appears to be just what I require, however require the same in VB.NET and am using inline coding rather than codebehind. Any ideas would be FAB. 5/4/2005 10:18 AM | Web'D

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Just what we needed. Quick and perfect. Thanx! 5/9/2005 1:51 PM | Luis Ferreira

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

add myself to the "just what I was looking for!| group ;-) 5/13/2005 11:10 AM | rafa

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Here is a routine that could help others based on your solution.

/// <summary>
/// Create the IFrame attributes for the Output
/// </summary>
/// <param name="key">The key</param>
/// <param name="reporttype">The type of Report being requested</param>
private void CreateSubReport(string key, string reporttype)
{
HtmlControl frame1 = (HtmlControl)this.FindControl("frame1");

string src = String.Concat("RateResultsReport.aspx?ID=", key, "&reporttype=", reporttype);
frame1.Attributes["src"] = src;
frame1.Attributes["width"] = "100%";
frame1.Attributes["height"] = "328px";

}
5/17/2005 3:27 PM | Tadd Stuart

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

man...congratiulations..very useful and simple! just what i needed! 5/21/2005 4:04 PM | b4silence

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

B.Tech fresher seeking a job in .NET domain, 5/29/2005 5:57 AM | MADHUSUDAN

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks! Really useful. You rule!

If I name the HTMLGenericControl with the same name as the IFrame I don't need to do the typecast and it works (VS .Net 2003). 6/3/2005 3:32 PM | Teo

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks! It worked good for me to. 6/11/2005 4:22 AM | Ian

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

THANKKKKKKKKSSSSSSSSs 6/13/2005 4:45 PM | Antonino (Argentina)

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

I have a scenario in which I am loading a separate classic-ASP application into an IFRAME which I have programmatically embedded into an ASP.Net web form. This external application uses session variables extensively, however, when it navigates from one page to another, it loses the session variables. Any suggestions? 6/21/2005 7:07 PM | David Kendrick

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

I have a scenario in which I am loading a separate classic-ASP application into an IFRAME which I have programmatically embedded into an ASP.Net web form. This external application uses session variables extensively, however, when it navigates from one page to another, it loses the session variables. Any suggestions? 6/21/2005 7:07 PM | David Kendrick

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Man, you should have a statue just for this article! I mean it! :D
I was searching for something like this for ages...
My most sincere thanks!
Cheers mate! 7/2/2005 1:58 AM | Vitor Mata

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

I tried you example, but for some reason, it throws an exception, "obj reference not set to an instance of an obj".

What could be causing the error?? 7/7/2005 6:34 AM | Vicky

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

WOW Great Dude, exactly what i was looking for, thank you very very much ..... 7/7/2005 9:15 AM | RAJAN

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

it doesnt give the Parent control.
If i write this.Parent.FindControl("frame1") it throws an exception Object reference not set to an instance 7/20/2005 10:54 AM | suparna

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Jus' what the patient needed..Thanks for ur help..this was a one stop look for exactly what i needed.. 8/2/2005 4:44 PM | Rishi

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Vicky : you didn´t set the tag to "runnat=server" 8/16/2005 12:44 PM | Markt

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks a lot for sharing this !!!!
It beat the previous method I was using (Client-side script)
I hope you don't mind if I put a link to your site in the "THANK YOU" section of mine !!! 8/20/2005 8:05 PM | MarianoK

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

And to say it once again!!!!! Thanks very much. I spent hours trying to deal with z-index to no avail. This code got me what I wanted in 10 minutes 8/27/2005 3:16 AM | Mark

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks!!!
How could I write a soap method response to it?
davidlarew@hotmail.com 9/9/2005 1:44 AM | David Larew

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

how to find control in VB syntax?
HtmlControl frame1 = (HtmlControl)this.FindControl("frame1")
Did anybody wrote in vb?
Cheers to Harish . Thanks for sharing great stuff. It sure saved a lot of strugles for many.
9/15/2005 2:34 PM | Danielle

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

how to find control in VB syntax?
HtmlControl frame1 = (HtmlControl)this.FindControl("frame1")
Did anybody wrote in vb?
Cheers to Harish . Thanks for sharing great stuff. It sure saved a lot of strugles for many.
9/16/2005 7:09 AM | help me

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Man your cool, Given exactly what i was looking for in the right context.

Cheers mate 9/19/2005 11:14 PM | Krishan Ariyawansa

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

This is a template for explaining any tech stuff. Nothing more, nothing less, Just perfect. Keep up the good work !!!!!! 9/22/2005 3:57 AM | sash

# how to use the user controls of base page where iFrame define re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

i want immidiate solution plz help me !! 9/22/2005 9:05 AM | shaan_26

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

The same code in VB.NET
Protected WithEvents frame1 As System.Web.UI.HtmlControls.HtmlGenericControl

Dim frame1 As HtmlControl = CType(Me.FindControl("frame1"), HtmlControl)
Dim src As String = String.Concat("RateResultsReport.aspx?ID=", key, "&reporttype=", reporttype)
With frame1
.Attributes("src") = src
.Attributes("width") = "100%"
.Attributes("height") = "328px"
End With 9/30/2005 1:32 PM | PhOeNiX

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

nice..I need it...and find it...thank u and google... 10/3/2005 8:17 AM | Arasu

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

thank for help 10/5/2005 5:05 AM | shafi ahmed

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanx...This was very useful.. 10/10/2005 7:28 PM | happy

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Great! I've been looking for this for weeks! 10/17/2005 9:14 AM | Jim

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Can't see the code :( 11/9/2005 9:33 PM | Paul Jones

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

can i adjust the height dinamically too?? 11/11/2005 5:44 AM | Albert

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Check one of the feedbacks above. You can do

frame1.Attributes["src"] = src;
frame1.Attributes["width"] = "100%";
frame1.Attributes["height"] = "328px";

You can change this dynamically in the code behind as you can see 11/11/2005 5:47 AM | Harish

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Excelente!!!, but how can hide IFRAME? 11/16/2005 3:04 PM | Gustavo

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

I can't see the code on this page properly for some reason. Can some please copy it and send it to me at jonesy_boy10@hotmail.com? Thanks heaps. 11/17/2005 9:10 PM | Paul Jones

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Sounds just what I'm looking for, but I can't see the code either! I would appreciate a copy at john@sides-pearson.supanet.com

11/18/2005 7:05 PM | John Pearson

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Magnifico!

Just what the doctor ordered! A simple, sturdy solution.

*pats author on back* 11/21/2005 6:34 PM | Mike McMorrow

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

I would like to know if i can set the innerText or innerHTML property dynamically just like frame1.Attributes["innerHTML"] = "<p>hello</p>";
It didnt work for me. pls help
12/1/2005 8:33 AM | Aftab

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Its really good article which i am looking for. 12/2/2005 2:12 PM | Anand Patil

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Ideal, simple and to the point. You solved my problem on first search took me 10 mins to fix find the solutions and implement it, excellent 12/8/2005 7:52 AM | JordansGhost

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

I am using a SPAW text editor, i want that the text edited with text editor placed on a page must appear in an iframe placed on the next page. How can this be done? 12/21/2005 1:29 AM | Rachita

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

I am using a SPAW text editor, i want that the text edited with text editor placed on a page must appear in an iframe placed on the next page. How can this be done? 12/21/2005 1:36 AM | Ricky

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

I could'nt get it to work so I did this:
<asp:Label id="lblIframe" runat="server"></asp:Label>

lblIframe.Text = "<iframe src=url></iframe>"; 1/12/2006 8:42 AM | virdun

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Excellent. Hard to believe something that should be so easy to do is difficult in ASP.Net. Why wouldn't the src be an attribute that ASP.Net would recognize or accept adding.

Thanks for the code. 1/19/2006 12:46 PM | Bruce

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks very much! Exactly what I'm looking for. Very useful! 1/23/2006 8:59 PM | Coder

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Anyone know if/how it is possible to inject the HTML into the response stream of the iframe. I want to build a page in memory as a text stream and dump it into the iframe. 1/31/2006 5:52 AM | Chris Davey

# Amethyst Rings, Amethyst Jewelry Amethyst Earrings

Amethyst Rings, Amethyst Earrings and Much More at MySolitaire.com 1/31/2006 6:12 AM | Megan

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hi,

Being new to ASP/C# does anyone know why I get an Object reference not set to an instance of an object exception when using the example code above?

Aspx file has: <iframe runat="server" id="image1" name="image1" src="" height="" width=""></iframe>

C# file has:

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

frame1.Attributes["src"] = "~/images/Test.gif";
frame1.Attributes["width"] = "100%";
frame1.Attributes["height"] = "100%";

Thanks in advance.

Andrew


2/16/2006 7:53 AM | Andrew Mercer

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

<Quote>
I could'nt get it to work so I did this:
<asp:Label id="lblIframe" runat="server"></asp:Label>

lblIframe.Text = "<iframe src=url></iframe>";
</quote>
That method also works but if you need to place two iframes on the same page and need them in a certain spot, it will not work. The label method will stack them on top of each other. 2/20/2006 2:46 PM | casey

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hey man, u have done a great job, this is what I was looking for. Keep on posting new things which will be helpful for us like this one. 4/3/2006 5:15 AM | Raj Shimpi

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Awesome code! Thanks for posting something usable! 4/14/2006 8:15 PM | Kenny

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

This is a great solution, but does not work with a Masterpage setup in asp.net. Any ideas why?

Frame1 = CType(Me.FindControl("Frame1"), HtmlControl)

returns nothing....

Email if someone has a solution....wendit@selltis.com 5/2/2006 7:25 PM | Wendi

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

It is possible programmatically access a label control which lives in my iframe? 5/4/2006 8:43 AM | iqbal

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

I'm having the same problem. It worked fine until I changed to using master pages, but then it stopped working. It can't find the control on the page. 5/8/2006 6:33 PM | ProphetCat

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

If you use MasterPage, the FindControl() has to be overridden to be recursive. 5/19/2006 6:38 PM | Huy

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

great!!! Keep helping people this way. 5/23/2006 7:04 PM | arvindc

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

I am not able to eecuting,when i execute this the browser page is not showing any thing(its in same page and task bar is blinking
can you please tell me
thanks 5/29/2006 10:32 AM | srini

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

I cannot see anything in the two frames. 6/1/2006 11:16 AM | Fred

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

First, many thanks for the code. Just what we needed.

I have a panel which contains the Iframe in the content area of the master page:

<asp:Content ID="Content1" ContentPlaceHolderID="PageContentPlaceHolder" Runat="Server">
<asp:Panel ID="Panel1" runat="server" Width="100%" Height="100%">
<iframe runat=server id="_content" name="_content" marginheight =0 marginwidth =0></iframe>
</asp:Panel>
</asp:Content>

I can access the Iframe by using:

Dim frame1 As HtmlControl = CType(Me.Panel1.FindControl("_content"), HtmlControl). This way I can send content there from a menu in the master page, by using a url= querystring:

frame1.Attributes("src") = ResolveUrl(Request.QueryString("url"))

However, I have 2 problems:

1) Mouseover on my TreeView menu, in the master page, causes the Iframe to redraw every time = very bad flickering.

2) I want to set the size of the Iframe to the size of the page it contains (within limits). I can set it to hard-coded values, but '100%' doesn't work at all.

i.e. frame1.Attributes("width") = "1000px" works but frame1.Attributes("width") = "100%" doesn't!

Does anyone have any solution for either of these problems?
6/6/2006 11:49 AM | Ray

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hi, I'mm having some issues with iframes. It is as follows:
I got these .ascx wich has an iframe on it pointing to another .ascx... I need some information that is produced in the ascx contained by the iframe to show it in the main control how can i get this information from the ascx running in the iframe???? 6/16/2006 3:22 PM | ABlanco

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Ray, maybe for your second problem you should use style for setting your page width. I haven't tried it but it appears in my property list. I would write it like this:

frame1.Attributes("style")="width:100%"

Hope it works 6/17/2006 1:20 PM | Offir

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hi everyone,

I have a page that is like a catalog. I need to load the item list from an iframe, and this works fine, but my problem is that I need to send a parameter from the iframe, to the parent page, so I can show the ID Card of the item that was clicked in the iframe. This means that I have to find a way to get the parameter in the parent page and do postback, to show the different ID cards.
Any ideas? 6/17/2006 1:29 PM | Offir

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hey dude! Fantastic!. Many thanks. 6/22/2006 2:01 PM | Sher.Net

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hey Guys, can you tell me how to print the iframe from code behind after assigning the "src" attribute. 7/7/2006 11:21 AM | Krishna

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

works great! here's a version i have modified that adds the entire iframe to the page dynamically, in case anyone is interested. you can run this script over and over again to add many iframes:

On the .aspx page, you'll need a placeholder(s) where the iframe(s) will be rendered:

<asp:PlaceHolder ID="theFrameHolder" runat="server" />


Then in the code behind, create and add the iframe to the placeholder:

HtmlControl frame1 = new System.Web.UI.HtmlControls.HtmlGenericControl("iframe");
frame1.Attributes["src"] = "http://search.msn.com";
frame1.Attributes["frameborder"] = "1";
frame1.Attributes["scrolling"] = "auto";

theFrameHolder.Controls.Add(frame1); 7/10/2006 5:44 PM | grateful

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks to everyone who has contributed to this page, especially last comment which really got this sorted for me. cheers!!! 7/13/2006 2:36 PM | Andy

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks for the comments.
The one by "grateful" has really been helpful and i am really Grateful to him/her.
Thanks. 7/13/2006 3:23 PM | Abhishek

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thank's, You save my vacantions ... 7/28/2006 7:53 AM | PAM

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

this is a way to dynamically set the height depending on the page content:

<script language="javascript" type="text/javascript">
function iFrameHeight() {
var h = 0;
if ( !document.all ) {
h = document.getElementById('iframe').contentDocument.height;
document.getElementById('iframe').style.height = h + 70 + 'px';
} else if( document.all ) {
h = document.frames('iframe').document.body.scrollHeight;
document.all.blockrandom.style.height = h + 30 + 'px';
}
}
</script>

<iframe id="iframe"
name="iframe"
src=""
width="100%"
height="500"
scrolling="auto"
frameborder="0"
class="wrapper"
allowTransparency="true"
runat="server"
>
This option will not work correctly. Unfortunately, your browser does not support Inline Frames
</iframe>

however because of the mix of javascript & runat=server, you'll also need this

(c#)
iframe.Attributes.Add("onload", "iFrameHeight();");

in the Page_Load or similar. 8/4/2006 6:10 AM | nickos

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks You save my Time 8/4/2006 8:56 AM | Kishore Kumar AVN

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

sorry, javascript in dynamic page should have been as follows

<script language="javascript" type="text/javascript">
function iFrameHeight() {
var h = 0;
if ( !document.all ) {
h = document.getElementById('iframe').contentDocument.height;
document.getElementById('iframe').style.height = h + 70 + 'px';
} else if( document.all ) {
h = document.frames('iframe').document.body.scrollHeight;
document.all.iframe.style.height = h + 30 + 'px';
}
}
</script> 8/7/2006 1:55 PM | nickos

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Too cool! This information on this page was extremely valuable! Excellent code example! 8/7/2006 7:42 PM | Ed McPeak

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks nickos it works really well. 8/25/2006 10:05 AM | Graham

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

This works great - has anyone had any browser issues(safari on the mac specifically) with Iframes 8/28/2006 5:10 PM | seand

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

when i open the doc,xls,and ppt file using iframe it shows open save dialog box.For me it should not show as such like when we open pdf file.How can we?...........Reply me its urgent...

Thanks in advance... 9/13/2006 4:51 AM | chandru

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

thanx a lot.. 9/13/2006 9:05 AM | Preethi

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

i am not able to set height dynamically even after using javascript
i am working in .net 2005 enviroment
my email is praveen.singh21@gmail.com
if u can help me it will be great 9/15/2006 4:36 AM | Praveen

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

How do I redirect the parent page from within the IFrame using codebehind (C#) 9/29/2006 5:57 AM | Jarred

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Did anyone ever find a way to communicate from within the iframe to the parent page (make it do a redirect, or anything for that matter)? 10/3/2006 12:08 PM | JeffOakman

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Andrew Mercer

I had the same problem as you with the Object reference not set to an instance of an object error. Here is what I did to get around that.

When you do the Protected WithEvents frame1... and then try to dim frame1 your get a problem - frame1 is already declared as 'Protected Dim with events... so you have to rename it. I did with MyFrame. This though led to the object error.

Therefore, take out the Dim line altogether. Then in a click event for something like a Link Button just put the line..

Me.frame1.Attributes("srd") = "http://whatever.com"

Note that the url must be enclosed in double quotes or you will get an http is not declared problem.

This is what did it for me. 10/7/2006 10:20 PM | Buz Meg Yall

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks folks.

It was really a good example. 10/27/2006 1:56 PM | Phoenixian

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks , this was great help. 11/1/2006 10:58 AM | Rachna

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

i cant see the code can anyone send me the code at register2spider@yahoo.com 11/1/2006 11:44 AM | vikas

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

any way bt which we can display two IFRAMES with a single IFRAME tag. the problem is like this. i have to enter text in a textbox. when i click a link an IFRAME should be opened and when i click the second link another IFRAME should be opened. both the IFRAMES should contain the text i enter in the textbox..
any ideas??? 11/10/2006 5:21 AM | ajay

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Does not work as data example code in the boxes is missing after first and second "as follows". Without knowing what this is, you cannot code this example. 11/24/2006 9:24 PM | J Leonard

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

its amaging...........it helps me a lot.........
thanks for the aricle and thanks for the coperation
Venu 12/19/2006 4:01 AM | venu gopal

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

The source frames are pointing to "about:blank" in IE. Not sure about FireFox. 1/3/2007 11:43 PM | John Baughman

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

ohhhhhhhhhhhhhhhhhhhhh
sorry i´m sorry

i´m so scarred with this solution !!!!

i din´t read the posts but if you put runat=server in a htmlcontrol in the html interface automaticaly you have this control in server side you don´t need a find control and bla bla bla all you have is something like that

html side
<iframe width="100%" height="100%" runat="server" id="name"/>

server side
this.name.addAttribut("src","url");

that´s all

sorry guys.. 1/16/2007 4:58 PM | 9

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

hi friends,
I am designing a web app using vb.net 2003 and i placed an iframe on aspx page. It works when i click "view in browser" but not when i press F5. I could not fix it.
Thanks in advance for your time,
Regards... 2/6/2007 8:17 PM | uyduruk

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hi,
I have 2 pages embedded in 2 iframes in my aspx page.
When I do a print preview, the content on the frame does not continue into the next page.

Does print only print one frame per page?

How can i make the frame to continue to the next page? Any ideas?

Thanks in advance!!!! 2/15/2007 12:35 PM | help

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

This code is great and easy to use... thanks... it works!!!!

"On the .aspx page, you'll need a placeholder(s) where the iframe(s) will be rendered:

<asp:PlaceHolder ID="theFrameHolder" runat="server" />


Then in the code behind, create and add the iframe to the placeholder:

HtmlControl frame1 = new System.Web.UI.HtmlControls.HtmlGenericControl("iframe");
frame1.Attributes["src"] = "http://search.msn.com";
frame1.Attributes["frameborder"] = "1";
frame1.Attributes["scrolling"] = "auto";

theFrameHolder.Controls.Add(frame1); " 2/26/2007 2:46 PM | DarkChuky

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

hi,
my problem is
i have a one ifram which have hyperlinks. when ever i click on that
it display contain of it in same ifram.
i want that data in other ifram .
so plz help me in this.
2/28/2007 10:26 AM | denish

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hi Thanks for posting such nice article on IFrame and also thanks to all persons who posted there comments on this page which really helps lot
But i have one problem when we use placeholder as given in above example use the same code
then how to adjust width and hight of the placeholder
pls help me 3/14/2007 6:46 AM | Nivrutti

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

i have tried the above code but my SlideMenu hides behind IFRAME at run time
can any one help me? 3/14/2007 7:44 AM | Nivrutti

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET



Realy informative.....

Thanks a lot 3/14/2007 10:22 AM | M N Anish

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

I am able to directly access the "src" property of the IFrame directly, without the requirement of the workaround provided here! But it's definitely a good information 3/19/2007 8:17 AM | Sam

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks to all of you on Excellent Job.
I want to use IFrame within a Update Panel of Atlas. But when different page loads it refresh the page. I am using asp.net Menu. On click of menu it loads the different page. Is it possible in Iframe it loads another page without refresh 4/10/2007 6:29 AM | Siddique

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Or you can do this :
<iframe id = "TreeTD" height="690px" src="<%=siteUrl%>" width="100%" frameborder = '0'></iframe>

siteUrl is a variable of your code behind 4/17/2007 5:05 AM | faouzi yassine

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

hey

HtmlControl frame1 = (HtmlControl)this.FindControl("frame1");
this is returning null??????
plz help me
umer.khan@systemsltd.com 5/2/2007 9:02 AM | umer

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

hey
i got frame but..........
i m stuck how to get control
i mean what i did

TextBox tx = new TextBox(); ;
HtmlControl frame1 = (HtmlControl)this.FindControl("frame1");
if (frame1 != null)
tx = (TextBox)frame1.FindControl ("txtFirst");

some one said, u cant find control from iframe , plz help me 5/2/2007 11:22 AM | umer

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Have just started a rewrite of some v v nasty code and thought I'd start by seeing if there was a better way to do this then we used. We had a placeholder and then wrote the html to it, I'm not a fan but this is a much better way, good work. 5/25/2007 10:06 AM | Matt

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hi thx U save me a lot of searching
8/10/2007 7:13 AM | demon

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

HtmlControl frame1 = (HtmlControl)this.FindControl("frame1") returns null. And declaring it in code behind as "frame1" same as the iframe id which is "frame1" gives an error. You cannot declare the same id for the same control on the same page.. literally, the code behind page and the source code page are the same. I tried using

this.frame1.Attributes["src"] = //url;

this worked for me. No other declaration, because the page already has the frame1 control on it. 9/20/2007 3:09 PM | NoName

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks, the code works great!

My problem is that I do not want the URL of the site in the iFrame to show up in the client-side source code.

In other words, the src attribute for the iframe should be blank, or otherwise obfuscated.

Is there any way to accomplish this?

Thanks in advance. 9/26/2007 7:07 AM | Seth

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Shweeet... thanks man. I'm sure this can be applied to a bunch of stuff.. thanks for the primer! Way to go. 12/6/2007 12:25 AM | Ozzie

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thank you,

it was really helpful for me:)
i generally use your site, when i am in truble.

Thanks and Regards,
Jyoti S Pokharkar.
asp.net developer,
IOL Broadband pvt ltd.
12/12/2007 7:18 PM | miss jyoti sopan pokharkar

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Just what I was looking for. Thank you very much. 12/13/2007 12:51 AM | Jon Hawkins

# rHow to Load Two IFRAME dynamically from codebehind - ASP.NET

I would like to know if we have two iframe in a same asp.net web page...how could we show the content of the two prame on page load. Please help 12/31/2007 4:02 AM | Neerman

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

how to show dynamically created xml file in iframe .The xml file is a string(in memory) and not saved as an xml file. 1/17/2008 9:16 PM | pankaj

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

how to write ifram in asp.net 1/21/2008 8:57 PM | suman

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

how to write ifram in asp.netfdslkfsdjflsadf 1/21/2008 8:59 PM | suman

# "How to update an iFrame upon a change event from a different iFrame?" - ASP.NET

Interesting Article.

It works on the SAME ASPX page. Major issue however is that it causes all Internet Browsers to blink it seems when an update occurs. (Internet Explorer 6.x, Internet Explorer 7.x, Firefox 2.x, Netscape 9.x, and Opera 9.x!)

I need to update another iFrame ONLY when the ASPX page that automatically determines there is a change (a newer picture, for example) that needs to update the other iFrame.

I have 2 asp pages that will do this now BUT ONLY in Internet Explorer 6 or Internet Explorer 7 there is a VERY annoying blink every time the page updates. Don't have this issue in these Internet Browsers Firefox, Netscape, or Opera.

Instead of checking for the update on the web page and updating the web page every say 30 seconds, only update when a change to the page occurs. So if the highest picture to be shown on a page is the same as the last time, don't update the iFrame.

I have all the logic that is needed EXCEPT for the call to update the other frame!

I have these 3 ASPX pages:

1. Driver ASPX file that builds the layout of the page with different iFrame ID's.
- Passes iFrame Name to Parser ASPX file.

2. Parser ASPX file that checks to see if there have been any changes that need to be handled.
- Uses standard META tag Refresh to do the looping and cookies to manage the variables.

3. Display ASPX file which shows the information.

The Parser ASPX file is what is not currently able to handle the reference to the existing iFrame which was created by the Driver ASPX file.

Perhaps this will better explain what I need to do:
http://forums.asp.net/t/1213208.aspx

This is at least a step in the right direction.

Thanks! 2/5/2008 6:15 AM | Bill New

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hi, Can anyone tell mw how to display a dynamically created pdf generating binary output to the iframe src without creating any pdf file physically on the disk. 2/6/2008 10:15 PM | LS

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

This was a great help, thanks 2/13/2008 12:52 PM | Smita

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Can I scroll the iframe to a certain position on the page?

Thanks! 2/18/2008 9:52 AM | bj

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

hey really thanks/
i was really working hard on this but u make it so simple. 2/21/2008 5:16 PM | Komila

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

hey really thanks
i was really working hard on this but u make it so simple. 2/21/2008 5:16 PM | Komila

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks It really works for me 2/25/2008 4:47 PM | Santosh

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Thanks, just to add, I spent a while wondering the find control wasnt working for me!!!

It was because I use a master page, see this link if you have the same problem

http://west-wind.com/weblog/posts/5127.aspx 3/4/2008 12:39 AM | Nick

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hey thank you very much,
this is an excellent piece of information.

cheers
3/7/2008 2:35 AM | kaumil

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Ya it is very help full for me any way thank u very much one small question why it not get src whenever it declared at clientside. 3/10/2008 8:28 PM | MohanRao Gundapaneni

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Many thanks, I have used in one wizard step, but I'm not able to make the next step. It's possible to close the iframe and send message next step from another iframe or page with c# or javascript without leave my page?. 3/11/2008 12:27 AM | Romi

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

gfghhgdhdfhf 3/12/2008 10:45 PM | hjh

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Excellent! Exactly what I was after! 3/13/2008 5:51 AM | Sean D

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

hi
We have our intranet website in cold fusion. I have developed some aspx pages for intranet.
For incorporating aspx pages in cold fusion I am using iframe on coldfusion side which calls aspx pages. Initially aspx page is loaded fine inside iframe. but when the button is clicked aspx pages are still opening but not in the context of the iframe. (I am using Response.Redirect to switch between pages internally). Guide me what I need to do in order to keep application functional inside iframe. 3/18/2008 2:24 AM | Noor Fazli

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Great! Just what I needed. Thanks. 3/19/2008 1:42 AM | Lynn R

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

thanx a lot for the iframe solution and the "iFrameHeight" solution too. excellent work 3/25/2008 8:19 AM | sunny j

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Hi,
have managed to create the iFrame but was not able to read the information in the iFrame.
Thanks a lot. 3/27/2008 8:01 PM | tw kan

# chat utility

SIR,
i WANT TO PROVIDE CHAT UTILITY IN MY SITE. HOW CAN I BUILD THAT CHAT UTILITY .PROVIDE ME IMPLEMENTATION DETAIL.I WAN TO IMPLEMENT IT USIONG ASP.NET ,.NET 2.0 FRAMEWORK 4/1/2008 9:16 PM | ASHISH MISHRA

# re: Loading pages in IFRAME dynamically from codebehind - ASP.NET

Rounding up the best deals and coupons on Dell, HP, Lenovo and Apple. http://consumercowboy.com 4/7/2008 4:34 AM | Austin O