Вы находитесь на странице: 1из 33

Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.

aspx

Project Cool
An ASP.NET Blog posts - 319, comments - 4690, trackbacks -
1010
Switch to Elastic Layout

<< Microsoft Innovation Days - Windows Vista, WPF/E, ASP.NET AJAX 1.0 | Home | Exploring ASP.NET
AJAX Webcast >>

Loading pages in IFRAME dynamically from codebehind - ASP.NET

Click here for a Video version of this Article

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:-

1 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

<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; (not required if working with ASP.NET 2.0, 3.5 i.e. Visual Studio
2005, 2008)

VB.NET

Protected WithEvents frame1 As System.Web.UI.HtmlControls.HtmlGenericControl (not required if working with ASP.NET 2.0, 3.5 i.e.
Visual Studio 2005, 2008)

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"); (not required if working with ASP.NET 2.0, 3.5 i.e. Visual Studio 2005,
2008)

2 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

VB.NET

Dim frame1 As HtmlControl = CType(Me.FindControl("frame1"), HtmlControl) (not required if working with ASP.NET 2.0, 3.5 i.e. Visual
Studio 2005, 2008)

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.

A related article on Triggering an event in the Parent Page from within Page inside IFRAME

Cheers !!!

posted @ Monday, April 25, 2005 8:03 AM

3 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

Comments on this entry:

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


Left by Thea Burger at 4/29/2005 6:25 AM

Exactly what I was looking for, thanks!!!!

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


Left by Lea McNabb at 5/3/2005 4:14 PM

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!

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


Left by Web'D at 5/4/2005 10:18 AM

Hi... .NET fresher here...this appears to be just what I require, however require the same in

4 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

VB.NET and am using inline coding rather than codebehind. Any ideas would be FAB.

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


Left by Luis Ferreira at 5/9/2005 1:51 PM

Just what we needed. Quick and perfect. Thanx!

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


Left by rafa at 5/13/2005 11:10 AM

add myself to the "just what I was looking for!| group ;-)

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


Left by Tadd Stuart at 5/17/2005 3:27 PM

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 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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


Left by b4silence at 5/21/2005 4:04 PM

man...congratiulations..very useful and simple! just what i needed!

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


Left by MADHUSUDAN at 5/29/2005 5:57 AM

B.Tech fresher seeking a job in .NET domain,

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


Left by Teo at 6/3/2005 3:32 PM

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

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


Left by Ian at 6/11/2005 4:22 AM

Thanks! It worked good for me to.

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


Left by David Kendrick at 6/21/2005 7:07 PM

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?

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


Left by David Kendrick at 6/21/2005 7:07 PM

6 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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?

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


Left by Vitor Mata at 7/2/2005 1:58 AM

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!

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


Left by Vicky at 7/7/2005 6:34 AM

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??

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


Left by RAJAN at 7/7/2005 9:15 AM

WOW Great Dude, exactly what i was looking for, thank you very very much .....

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


Left by suparna at 7/20/2005 10:54 AM

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 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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


Left by Rishi at 8/2/2005 4:44 PM

Jus' what the patient needed..Thanks for ur help..this was a one stop look for exactly what i
needed..

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


Left by Markt at 8/16/2005 12:44 PM

Vicky : you didn´t set the tag to "runnat=server"

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


Left by MarianoK at 8/20/2005 8:05 PM

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 !!!

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


Left by Mark at 8/27/2005 3:16 AM

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

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


Left by David Larew at 9/9/2005 1:44 AM

Thanks!!!
How could I write a soap method response to it?
davidlarew@hotmail.com

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


Left by Danielle at 9/15/2005 2:34 PM

8 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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.

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


Left by help me at 9/16/2005 7:09 AM

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.

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


Left by Krishan Ariyawansa at 9/19/2005 11:14 PM

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

Cheers mate

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


Left by sash at 9/22/2005 3:57 AM

This is a template for explaining any tech stuff. Nothing more, nothing less, Just perfect. Keep
up the good work !!!!!!

# how to use the user controls of base page where iFrame define re: Loading pages in IFRAME
dynamically from codebehind - ASP.NET
Left by shaan_26 at 9/22/2005 9:05 AM

i want immidiate solution plz help me !!

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

9 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

Left by PhOeNiX at 9/30/2005 1:32 PM

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

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


Left by Arasu at 10/3/2005 8:17 AM

nice..I need it...and find it...thank u and google...

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


Left by shafi ahmed at 10/5/2005 5:05 AM

thank for help

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


Left by happy at 10/10/2005 7:28 PM

Thanx...This was very useful..

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


Left by Jim at 10/17/2005 9:14 AM

Great! I've been looking for this for weeks!

10 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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


Left by Paul Jones at 11/9/2005 9:33 PM

Can't see the code :(

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


Left by Albert at 11/11/2005 5:44 AM

can i adjust the height dinamically too??

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


Left by Harish at 11/11/2005 5:47 AM

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

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


Left by Gustavo at 11/16/2005 3:04 PM

Excelente!!!, but how can hide IFRAME?

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


Left by Paul Jones at 11/17/2005 9:10 PM

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.

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


Left by John Pearson at 11/18/2005 7:05 PM

11 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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

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


Left by Mike McMorrow at 11/21/2005 6:34 PM

Magnifico!

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

*pats author on back*

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


Left by Aftab at 12/1/2005 8:33 AM

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

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


Left by Anand Patil at 12/2/2005 2:12 PM

Its really good article which i am looking for.

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


Left by JordansGhost at 12/8/2005 7:52 AM

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

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


Left by Rachita at 12/21/2005 1:29 AM

12 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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?

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


Left by Ricky at 12/21/2005 1:36 AM

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?

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


Left by virdun at 1/12/2006 8:42 AM

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>";

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


Left by Bruce at 1/19/2006 12:46 PM

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.

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


Left by Coder at 1/23/2006 8:59 PM

Thanks very much! Exactly what I'm looking for. Very useful!

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


Left by Chris Davey at 1/31/2006 5:52 AM

13 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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.

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


Left by Andrew Mercer at 2/16/2006 7:53 AM

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

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


Left by casey at 2/20/2006 2:46 PM

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

14 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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.

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


Left by Raj Shimpi at 4/3/2006 5:15 AM

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.

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


Left by Kenny at 4/14/2006 8:15 PM

Awesome code! Thanks for posting something usable!

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


Left by Wendi at 5/2/2006 7:25 PM

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

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


Left by iqbal at 5/4/2006 8:43 AM

It is possible programmatically access a label control which lives in my iframe?

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

15 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

Left by ProphetCat at 5/8/2006 6:33 PM

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.

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


Left by Huy at 5/19/2006 6:38 PM

If you use MasterPage, the FindControl() has to be overridden to be recursive.

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


Left by arvindc at 5/23/2006 7:04 PM

great!!! Keep helping people this way.

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


Left by srini at 5/29/2006 10:32 AM

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

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


Left by Fred at 6/1/2006 11:16 AM

I cannot see anything in the two frames.

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


Left by Ray at 6/6/2006 11:49 AM

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

16 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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?

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


Left by ABlanco at 6/16/2006 3:22 PM

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

17 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

get this information from the ascx running in the iframe????

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


Left by Offir at 6/17/2006 1:20 PM

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

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


Left by Offir at 6/17/2006 1:29 PM

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?

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


Left by Sher.Net at 6/22/2006 2:01 PM

Hey dude! Fantastic!. Many thanks.

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


Left by Krishna at 7/7/2006 11:21 AM

Hey Guys, can you tell me how to print the iframe from code behind after assigning the "src"
attribute.

18 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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


Left by grateful at 7/10/2006 5:44 PM

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);

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


Left by Andy at 7/13/2006 2:36 PM

Thanks to everyone who has contributed to this page, especially last comment which really got
this sorted for me. cheers!!!

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


Left by Abhishek at 7/13/2006 3:23 PM

Thanks for the comments.


The one by "grateful" has really been helpful and i am really Grateful to him/her.
Thanks.

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

19 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

Left by PAM at 7/28/2006 7:53 AM

Thank's, You save my vacantions ...

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


Left by nickos at 8/4/2006 6:10 AM

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>

20 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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.

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


Left by Kishore Kumar AVN at 8/4/2006 8:56 AM

Thanks You save my Time

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


Left by nickos at 8/7/2006 1:55 PM

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>

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


Left by Ed McPeak at 8/7/2006 7:42 PM

21 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

Too cool! This information on this page was extremely valuable! Excellent code example!

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


Left by Graham at 8/25/2006 10:05 AM

Thanks nickos it works really well.

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


Left by seand at 8/28/2006 5:10 PM

This works great - has anyone had any browser issues(safari on the mac specifically) with
Iframes

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


Left by chandru at 9/13/2006 4:51 AM

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

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


Left by Preethi at 9/13/2006 9:05 AM

thanx a lot..

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


Left by Praveen at 9/15/2006 4:36 AM

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

22 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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


Left by Jarred at 9/29/2006 5:57 AM

How do I redirect the parent page from within the IFrame using codebehind (C#)

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


Left by JeffOakman at 10/3/2006 12:08 PM

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)?

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


Left by Buz Meg Yall at 10/7/2006 10:20 PM

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.

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


Left by Phoenixian at 10/27/2006 1:56 PM

23 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

Thanks folks.

It was really a good example.

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


Left by Rachna at 11/1/2006 10:58 AM

Thanks , this was great help.

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


Left by vikas at 11/1/2006 11:44 AM

i cant see the code can anyone send me the code at register2spider@yahoo.com

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


Left by ajay at 11/10/2006 5:21 AM

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???

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


Left by J Leonard at 11/24/2006 9:24 PM

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.

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


Left by venu gopal at 12/19/2006 4:01 AM

its amaging...........it helps me a lot.........

24 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

thanks for the aricle and thanks for the coperation


Venu

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


Left by John Baughman at 1/3/2007 11:43 PM

The source frames are pointing to "about:blank" in IE. Not sure about FireFox.

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


Left by 9 at 1/16/2007 4:58 PM

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

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


Left by uyduruk at 2/6/2007 8:17 PM

hi friends,

25 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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

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


Left by help at 2/15/2007 12:35 PM

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!!!!

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


Left by DarkChuky at 2/26/2007 2:46 PM

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";

26 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

theFrameHolder.Controls.Add(frame1); "

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


Left by denish at 2/28/2007 10:26 AM

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.

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


Left by Nivrutti at 3/14/2007 6:46 AM

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

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


Left by Nivrutti at 3/14/2007 7:44 AM

i have tried the above code but my SlideMenu hides behind IFRAME at run time
can any one help me?

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


Left by M N Anish at 3/14/2007 10:22 AM

Realy informative.....

27 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

Thanks a lot

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


Left by Sam at 3/19/2007 8:17 AM

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

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


Left by Siddique at 4/10/2007 6:29 AM

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

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


Left by faouzi yassine at 4/17/2007 5:05 AM

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

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


Left by umer at 5/2/2007 9:02 AM

hey

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


this is returning null??????
plz help me
umer.khan@systemsltd.com

28 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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


Left by umer at 5/2/2007 11:22 AM

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

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


Left by Matt at 5/25/2007 10:06 AM

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.

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


Left by demon at 8/10/2007 7:13 AM

Hi thx U save me a lot of searching

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


Left by NoName at 9/20/2007 3:09 PM

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

29 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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

this worked for me. No other declaration, because the page already has the frame1 control on
it.

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


Left by Seth at 9/26/2007 7:07 AM

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.

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


Left by Ozzie at 12/6/2007 12:25 AM

Shweeet... thanks man. I'm sure this can be applied to a bunch of stuff.. thanks for the primer!
Way to go.

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


Left by miss jyoti sopan pokharkar at 12/12/2007 7:18 PM

Thank you,

it was really helpful for me:)


i generally use your site, when i am in truble.

30 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

Thanks and Regards,


Jyoti S Pokharkar.
asp.net developer,
IOL Broadband pvt ltd.

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


Left by Jon Hawkins at 12/13/2007 12:51 AM

Just what I was looking for. Thank you very much.

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


Left by Neerman at 12/31/2007 4:02 AM

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

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


Left by pankaj at 1/17/2008 9:16 PM

how to show dynamically created xml file in iframe .The xml file is a string(in memory) and not
saved as an xml file.

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


Left by suman at 1/21/2008 8:57 PM

how to write ifram in asp.net

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


Left by suman at 1/21/2008 8:59 PM

how to write ifram in asp.netfdslkfsdjflsadf

# "How to update an iFrame upon a change event from a different iFrame?" - ASP.NET
Left by Bill New at 2/5/2008 6:15 AM

31 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

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

32 of 33 10/7/2010 6:19 PM
Loading pages in IFRAME dynamically from codebehind - ASP.NET http://geekswithblogs.net/ranganh/archive/2005/04/25/37635.aspx

This is at least a step in the right direction.

Thanks!

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


Left by LS at 2/6/2008 10:15 PM

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.

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


Left by Smita at 2/13/2008 12:52 PM

This was a great help, thanks

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


Left by bj at 2/18/2008 9:52 AM

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

Thanks!

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


Left by Komila at 2/21/2008 5:16 PM

Gravatar

33 of 33 10/7/2010 6:19 PM

Вам также может понравиться