Loading pages in IFRAME dynamically from codebehind - ASP.NET

 

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

Cheers !!!

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

Print

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

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

Left by Luis Ferreira at 5/9/2005 1:51 PM
Gravatar
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
Gravatar
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
Gravatar
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";

}

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

Left by b4silence at 5/21/2005 4:04 PM
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
it doesnt give the Parent control.
If i write this.Parent.FindControl("frame1") it throws an exception Object reference not set to an instance

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

Left by Rishi at 8/2/2005 4:44 PM
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
i want immidiate solution plz help me !!

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

Left by PhOeNiX at 9/30/2005 1:32 PM
Gravatar
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
Gravatar
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
Gravatar
thank for help

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

Left by happy at 10/10/2005 7:28 PM
Gravatar
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
Gravatar
Great! I've been looking for this for weeks!

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

Left by Paul Jones at 11/9/2005 9:33 PM
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
<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.

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

Left by Raj Shimpi at 4/3/2006 5:15 AM
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
It is possible programmatically access a label control which lives in my iframe?

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

Left by ProphetCat at 5/8/2006 6:33 PM
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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?

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

Left by ABlanco at 6/16/2006 3:22 PM
Gravatar
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????

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

Left by Offir at 6/17/2006 1:20 PM
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
Hey Guys, can you tell me how to print the iframe from code behind after assigning the "src" attribute.

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

Left by grateful at 7/10/2006 5:44 PM
Gravatar
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
Gravatar
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
Gravatar
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

Left by PAM at 7/28/2006 7:53 AM
Gravatar
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
Gravatar
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.

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

Left by Kishore Kumar AVN at 8/4/2006 8:56 AM
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
thanx a lot..

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

Left by Praveen at 9/15/2006 4:36 AM
Gravatar
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

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

Left by Jarred at 9/29/2006 5:57 AM
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
its amaging...........it helps me a lot.........
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
Gravatar
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
Gravatar
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
Gravatar
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...

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

Left by help at 2/15/2007 12:35 PM
Gravatar
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
Gravatar
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); "

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

Left by denish at 2/28/2007 10:26 AM
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar


Realy informative.....

Thanks a lot

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

Left by Sam at 3/19/2007 8:17 AM
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
hey

HtmlControl frame1 = (HtmlControl)this.FindControl("frame1");
this is returning null??????
plz help me
umer.khan@systemsltd.com

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

Left by umer at 5/2/2007 11:22 AM
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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.

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

Left by Seth at 9/26/2007 7:07 AM
Gravatar
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
Gravatar
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
Gravatar
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.

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

Left by Jon Hawkins at 12/13/2007 12:51 AM
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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
Gravatar
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!

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

Left by LS at 2/6/2008 10:15 PM
Gravatar
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
Gravatar
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
Gravatar
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
hey really thanks/
i was really working hard on this but u make it so simple.

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

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

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

Left by Santosh at 2/25/2008 4:47 PM
Gravatar
Thanks It really works for me

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

Left by Nick at 3/4/2008 12:39 AM
Gravatar
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

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

Left by kaumil at 3/7/2008 2:35 AM
Gravatar
Hey thank you very much,
this is an excellent piece of information.

cheers

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

Left by MohanRao Gundapaneni at 3/10/2008 8:28 PM
Gravatar
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.

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

Left by Romi at 3/11/2008 12:27 AM
Gravatar
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?.

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

Left by Sean D at 3/13/2008 5:51 AM
Gravatar
Excellent! Exactly what I was after!

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

Left by Noor Fazli at 3/18/2008 2:24 AM
Gravatar
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.

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

Left by Lynn R at 3/19/2008 1:42 AM
Gravatar
Great! Just what I needed. Thanks.

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

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

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

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

# chat utility

Left by ASHISH MISHRA at 4/1/2008 9:16 PM
Gravatar
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

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

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

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

Left by Chitta at 4/14/2008 9:20 AM
Gravatar
Hi,

Thanks for this great article.
But i have a problem when trying to load .htm files into the frame.

The text in .htm file is getting wraapped while loading into the frame..

Any suggestions please..
Thanks in advance.....

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

Left by asdf at 4/22/2008 2:35 AM
Gravatar
You can also use:
<iframe id=frame src="<%= sourcePath %>"></iframe>

Then in your code behind, set the sourcePath variable to whatever you want.

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

Left by yemek tarifleri at 4/23/2008 2:56 AM
Gravatar
Good articles

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

Left by yemek tarifi at 4/23/2008 2:56 AM
Gravatar
hey really thanks

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

Left by Ismail Farajallah at 4/27/2008 9:39 PM
Gravatar
THANKS..
YOU ARE THE BEST!..

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

Left by purusoth at 5/7/2008 1:28 AM
Gravatar
Excuse me guys... I can load the file through <IFRAME>, I want to edit that file and save application directory... This is my problem... Please help me..

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

Left by Steve Someone at 5/13/2008 4:07 AM
Gravatar
Putting hotkeys on the form fields in this page really sucks. Every time I try to search for a word in your page by using the Internet Explorer hotkeys I get pulled down to this Post A Comment form. Boooooo! You're trying to be too clever.

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

Left by Gayathri at 5/14/2008 9:07 PM
Gravatar
Oh Thanks so much for this article! Extremely helpful. :)

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

Left by webmonkeymon at 5/28/2008 12:39 PM
Gravatar
thanks! works great. Here is a vb version of Dark chuky's post.

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

in the code behind---

Dim frame1 As HtmlControl
frame1 = New System.Web.UI.HtmlControls.HtmlGenericControl("iframe")

frame1.Attributes.Add("src", "http://www.mywebsite.com")

frame1.Attributes.Add("frameborder", "1")

frame1.Attributes.Add("scrolling", "auto")
frame1.Attributes.Add("width", "100%")
frame1.Attributes.Add("height", "300") theFrameHolder.Controls.Add(frame1)

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

Left by Fabian at 6/1/2008 11:50 PM
Gravatar
Thank you very much. It helps me alot!

Greetings from Germany

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

Left by Jyothi at 6/3/2008 11:14 PM
Gravatar
thanks alot. i could open the pdf file through iframe in aspx page only. but i could not able to open doc files through iframe with in the aspx page only. i is asking to open/save/close, then doc is opening separately.
but i want i should open with in aspx page only. how to get it through iframes.

thanks

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

Left by Gary at 6/5/2008 7:53 AM
Gravatar
First, thanks for the code. It's GREAT!

Does anyone know if it's possible to:
1) Set the iframe source in code behind as shown here.
2) If the user clicks a link in the page shown in the iframe, "catch" the event in the code-behind.
3) Determine the url that the iframe was being directed to.

Would really appreciate any help if someone has done this.

Thanks

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

Left by Mohamed Gouda at 6/10/2008 9:28 PM
Gravatar
It can be done more straight forward if you set the data type of the IFrame as HtmlControl
instead of HtmlGenericControl
Now, no need for casting and no need for redundant variables

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

Left by sherpa at 6/12/2008 12:02 PM
Gravatar
I want to use the iframe to be flush gainst the top of the browser window, but there is a an empty spcae of about of atleast 10 pixels before the iframe starts. Anyway to make it flush against the browser window.

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

Left by Nizar at 6/24/2008 5:19 PM
Gravatar
Hi Guys,

I have a similar scenario, I assume that this article will load particular website e.g. "www.yahoo.com" using iframe dynamically from the code behind. But, I want to make a kind of template where I can just change the desired website via the web address. for example: www.mywebsite.com?vacatures.aspx?<URL>. This <URL> refers to the website. So, in this case it will be: www.mywebsite.com?vacatures.aspx?http://www.yahoo.com

Please I am looking forward to hearing from all of you. Many thanks.

Grtz,nizar

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

Left by Sam P at 6/30/2008 1:22 AM
Gravatar
A couple of comments.

I was getting a null exception ("not set to an instance of an object") and realised it was because my control was in a panel. I had to do (HtmlControl)this.MyPanel.FindControl("frame1");

@Sherpa, you need to change page margins: http://www.w3schools.com/CSS/css_margin.asp Hope you're not going to use this to trick your users, though!

@Nizar, you want to use an address like www.mywebsite.com/vacatures.aspx?iframeurl=http://www.yahoo.com and then in your code behind you can refer to Request.QueryString["iframeurl"].

Hope these help.

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

Left by Sam P at 6/30/2008 1:22 AM
Gravatar
A couple of comments.

I was getting a null exception ("not set to an instance of an object") and realised it was because my control was in a panel. I had to do (HtmlControl)this.MyPanel.FindControl("frame1");

@Sherpa, you need to change page margins: http://www.w3schools.com/CSS/css_margin.asp Hope you're not going to use this to trick your users, though!

@Nizar, you want to use an address like www.mywebsite.com/vacatures.aspx?iframeurl=http://www.yahoo.com and then in your code behind you can refer to Request.QueryString["iframeurl"].

Hope these help.

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

Left by Matt at 7/28/2008 7:12 AM
Gravatar
aspx code:
<IFRAME id="ifMyFrame" runat="server"></IFRAME>

c# code:
ifMyFrame.Attributes.Add("src", "http://www.mypage.com");

That's all!

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

Left by M3mph15 at 8/4/2008 3:07 PM
Gravatar
Ok, i have a little problem and hopefully its easily fixed.

Ok i have a Default.aspx page with three iframes on it. frame1, frame2, frame3. Now i have set up that when Default.aspx loads frame1 contains top.aspx. Which has a dropdownlist in it.

What i want is to select an option from the dropdownlist and according to the selection bring up the matching page in frame2.

I am using VB.Net to write my code behind.

I have placed the sample code for the iframe in the top.aspx code behind but when i run it frame2 has a NullReferenceException like it cant find the frame.

Have i placed the code in the wrong code behind? is it supposed to be in the Default.aspx code behind? if so how can i change the src of frame2 to whatever is selected in frame1.

Any help is much appreciated

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

Left by ML at 8/6/2008 7:14 AM
Gravatar
I am an anyalst, not a programmer, working on a project that needs an iframe solution for displaying a regulated pdf. For both a portal application and a cold fusion application we need to have the same functionality due to the steps in our process. Those step do not allow the pdf to be printed too early in the process so we need to prevent that from occurring. I am being told by our development staff that, currently, they don't know how to shut off the adobe print funcationality in the iframe or by right clicking the mouse, and are seeing what they can find out. I thought I'd ask you if you knew a way to stop print functionality in an iframe for a pdf document. Please help!

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

Left by umesh at 8/11/2008 6:26 PM
Gravatar
hi i have a problem that i use ifrmae and fill it dynamically from database in datalist form when i click on link button then open new page but that page is open within ifrmae i want to build a new page. how can i solve this problem.

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

Left by JP at 8/12/2008 8:15 AM
Gravatar
Thanks! This was accurate, and it worked perfect.
Took me two seconds to implement code behind which displays a specific page in an iframe based on user's input. Very helpful.

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

Left by Hammer at 8/15/2008 5:37 AM
Gravatar
Hi I just wanted to say that I used the example in an AJAX.NET form with Master/Content pages but I wasn't able to get it to work without placing this code in the page load. I need the iframe to load on review button click because the user has a choice of multiple images. The Ajax postback doesn't cycle IFRAME load, so I was struck. I resolve my issue by doing 2 important things.

In the Page_Load
iframe.Load += new EventHandler(iframe_Load);
added a iframe load handler

In Ajax form
<asp:UpdatePanel ID="UpdatePane3" runat="server" RenderMode="Inline">
<ContentTemplate>
<iframe class="visible" id="iframe" runat="server" ></iframe>
</ContentTemplate>
</asp:UpdatePanel>

I added my IFRAME so when review button postback occurs my src is updated and a new image will display each time.

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

Left by shaju at 8/31/2008 10:47 PM
Gravatar
c once u declare iframe as server control there is no meaning in declaring again,u can directly cakll iframe.Attributes("src")

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

Left by ColDek98 at 9/4/2008 4:14 AM
Gravatar
This is exactly what I was looking for. Thanks for the post!

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

Left by Mike McCann at 9/6/2008 11:04 PM
Gravatar
Hi,

When I try this in VS2008 I've only got a partial class so I can't define
Protected WithEvents frame1 As System.Web.UI.HtmlControls.HtmlGenericControl

and if I leave that line out then the control isn't found on the

Dim showDynamicPage As HtmlControl
showDynamicPage = CType(Me.FindControl("showDynamicPage"), HtmlControl)

showDynamicPage.Attributes("src") = "http://www.live.com"

Thanks
Mike

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

Left by Mike McCann at 9/6/2008 11:52 PM
Gravatar
Sorry about that last post
I'ts working
Mike

# You're the Bomb

Left by Godwin at 9/18/2008 2:44 AM
Gravatar
I think I'd need to bookmark this page immediately.... this solves my challenge, just the way I like it... thanks

# word doc file in iframe in asp.net and #

Left by Rrahul at 9/25/2008 7:04 PM
Gravatar
I want to diaply a word doc file in iframe using asp.net and c# and the height of iframe should be set according to the content of doc file

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

Left by rahul at 10/3/2008 1:24 AM
Gravatar
I have aspx page. Inside a div, it has a iframe which has src attribute. iframe hosts the htm file web page.

I need a way to transport variable values between the aspx and htm page on button clicks.

How do I do that?

I need a way so that using javascript I can read the value in the div - iframe - htm page objects.

Please help.

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

Left by Nguyen at 10/8/2008 7:18 PM
Gravatar
If you have the control in the ASPX page with the attribute runat="server", you can use its ID directly on your page_load event on the code-behind. Here is a c# example:

protected void Page_Load(object sender, EventArgs e)
{
MyIframe.Attributes.Add("src", myURLstring);
}

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

Left by Mauricio Garcia at 10/10/2008 7:10 AM
Gravatar
Thanks for de example code!!!!!

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

Left by Venkat at 10/21/2008 10:56 AM
Gravatar
Reaally nice.Thank you very much

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

Left by Shimmy at 10/29/2008 2:34 AM
Gravatar
Thank you very much!!!

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

Left by Rathish at 11/4/2008 3:38 PM
Gravatar
Thanks youuuuuuuuuu

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

Left by Losing session state at 11/6/2008 6:57 PM
Gravatar
Has anyone resolved the problem of losing session state when doing this?

The question was first asked by David Kendrick back in 2005 but I never saw a response and I'm having the exact same issue...

"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

Left by Losing session state at 11/6/2008 11:54 PM
Gravatar
As a followup, it seems session state is not lost if masking is not enabled. Domain forwarding with masking loses session state between pages, but domain forwarding by itself does not.

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

Left by JK at 11/8/2008 2:54 AM
Gravatar
Hi, Thank you for answer. Is there anyway to set a gridview control dynamically as the iframe's src through server side code? I need to set the InnerHtml prop of a <div> and have successfully used a handler for other files (jpg etc) for my iframe's src, but sometimes will need to show a gridview instead. Any help/ideas would be appreciated! Thanks!

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

Left by şişme bebek at 11/30/2008 6:46 AM
Gravatar
Hi, Thank you for answer. Is there anyway

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

Left by azdırıcı at 11/30/2008 3:57 PM
Gravatar
Hi, Thank you for good

# re: IS EVERY THING OK?

Left by edison at 12/3/2008 6:07 PM
Gravatar
so hows your mum ...is she still ok ... heard from your dad that she knock down by a car... is it very serious ... do you need my help i can always help you =) find the cheapest wake as well as coffin

don't be sad cheer
no worries

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

Left by michael jackson at 12/3/2008 6:15 PM
Gravatar
Shes fine not dead yet. Hey where this wake deal u got? Call my hp at 62353535. Thanks. Oh ya and my hse sell pizza for a very good deal. My mom like to talk like a reception when she picks up the phone. Don't worry you can just place your order, We get it delivered to anywhere just like Fedex.

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

Left by battery at 12/10/2008 7:47 PM
Gravatar

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

Left by rathan at 12/16/2008 11:42 PM
Gravatar
hi,
i want to show a directory in a iframe using asp.net and vb
can any one suggest me about this?
how to resolve this?

thanks in advance
rathan

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

Left by Rob at 12/19/2008 5:11 PM
Gravatar
Hi,

ust wanted to say thanks for making this process simple and easy to understand.

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

Left by sex at 12/27/2008 4:35 PM
Gravatar
thanks :) Good text.

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

Left by battery at 1/10/2009 12:20 PM
Gravatar
thanks in advance

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

Left by Rocky at 1/21/2009 9:44 PM
Gravatar
webmonkeymon's solution was perfect for me, thanks a million.

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

Left by mst at 1/22/2009 10:15 PM
Gravatar
still the best article on this. agree a statue should be built in your honor. thanks!

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

Left by Elavarasu at 1/27/2009 5:53 PM
Gravatar
Thanks for ur article. his is nice article, very helpful to me.

Thanks once again.

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

Left by Elisa at 2/14/2009 4:48 AM
Gravatar
Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!

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

Left by Frankie at 2/16/2009 9:34 PM
Gravatar
Hi,

I am using the same concept to open a temporary PDF file in a iFrame on my aspx page. the issue I am facing is that I can point the iframe to open a file out of the web site. for example, I can load the src attribute with an address of 'Temp/tmp435.PDF' which is within the virtual directory in IIS, but it does not work if I set the src with 'file:///c:/temp/tmp435.PDF'

Does anyone has an idea why I cant access a file out of the virtual directory?

any help is appreciated,

Thanks,

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

Left by Dan at 2/24/2009 1:34 AM
Gravatar
I am doing this with multiple IFRAMES on the same page it it hangs IE regularly. Has anyone else seen this issue or know a workaround?

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

Left by vahid at 2/24/2009 4:03 PM
Gravatar
still useful for me in 2009.
thanks.

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

Left by Dumin at 3/4/2009 11:14 AM
Gravatar
Thanks , nice explanation

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

Left by Blue at 3/12/2009 2:17 AM
Gravatar
Thanks! Still relevant and useful, years later :)

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

Left by nihal at 3/12/2009 12:45 PM
Gravatar

while developing a asp page,some of the tools are iframe and panel


iframe is like a web browser which is used to display the webpage contents, links, etc....

whenever any link is opened on the same iframe, the page will be loaded on the same page. but we are unable to obtain the url of the current loaded page.

We need a solution for this...........

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

Left by dll at 3/14/2009 8:16 PM
Gravatar
For both a portal application and a cold fusion application we need to have the same functionality due to the steps in our process. Those step do not allow the pdf to be printed too early in the process so we need to prevent that from occurring.

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

Left by Suresh R Kumar at 3/18/2009 6:29 PM
Gravatar
I am stuck with this Hyperlink, Javascript, IFrame, Asp.net.

I am dynamically creating a Hyper link from the database. I have IFrame the same page. When I click on the Hyperlink, it should call an HTML file and load that into the IFrame. Is that possible? Does this make sense?

Suresh - skumarrk@gmail.com

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

Left by Josh at 3/18/2009 11:42 PM
Gravatar
Genius. Nice work.

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

Left by hir at 3/28/2009 12:40 PM
Gravatar
thanks it helps alot

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

Left by Balu Gupta at 4/2/2009 3:48 PM
Gravatar
Excellent Boss
Thank you very much
I am trying for this code from a long time.

Thanks A Lot

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

Left by shophola at 4/2/2009 5:15 PM
Gravatar
Excellent

but do you have any example in php

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

Left by ak at 4/3/2009 8:20 PM
Gravatar
Thanks Excellent code. Saved a lot of time.

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

Left by Recep SELLİ at 4/3/2009 8:57 PM
Gravatar
Hey bro thank you very much :)

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

Left by Smita S pawar at 4/7/2009 2:01 PM
Gravatar
Hi The height of the iframe can be dynamically set using the following javascript:
function resizeIframe()
{
var height = document.frames('frameOutage').document.body.scrollHeight;
height += document.getElementById('frameOutage').offsetTop;
document.getElementById('frameOutage').style.height = height +"px";
}

<iframe id="frameOutage" scrolling="no" onload="return resizeIframe();"
width="100%" frameborder="0" runat="server"></iframe>
Cheers,
Smita S pawar

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

Left by Ravikrishna at 4/9/2009 9:35 AM
Gravatar
Hi,

it was gr8 article.. it helped me alot. but i got one problem here is, I am not able to open PDF files using IFRAME, I am able to open only MS-office files.
can any one help me in this regard.

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

Left by Dhruv at 4/17/2009 1:31 PM
Gravatar
thanks,
your article forced me to use an iframe and apply navigations to web-pages.
Truly, ur article is good for non-iframe-users.

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

Left by anna at 5/22/2009 6:34 PM
Gravatar
hi! its the best code i found and the code sample its very clear but i m not familiar using asp ...so.. i have a small problem...when the second page "www.live.com" is loading hyperlinks and photos which are on it are not active/visible. its just the text. (if i dont load the second page "www.live.com" using the iframe everything works right). any help???
thank u very much in advance

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

Left by avheatherim at 6/1/2009 8:46 PM
Gravatar
Thanks so much! This was exactly what I needed!!!

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

Left by Anushka Thilakarathne at 6/2/2009 9:55 AM
Gravatar
thanks you. i was looking for this

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

Left by Hemant Gaur at 6/8/2009 3:46 PM
Gravatar
Hello, I am trying to load a PDF in an iframe. Your technique used to work in IE7. Just upgraded to IE8, and is not working since then. It pops up another window and opens the pdf in that instance. Any ideas on what I can do to fix it? Would much appreciate. Thank you.

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

Left by Leke at 6/22/2009 12:14 PM
Gravatar
This example did not work with iframe used along with modalpopupextender control using asp.net 2.0. I have trying to change the source attribute of the iframe at the page_load event. what can be done?

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

Left by Mohammed Habeeb at 6/23/2009 8:12 AM
Gravatar
Thanks Dude. Exactly what I was looking for.

Your comment:



 (will not be displayed)


 
 
 
 

Live Comment Preview:

 
«July»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678