Using iFrames In ASP.NET
Asked Answered
C

4

22

I have an asp.net website with a master-page, can I use the iframe so my .aspx pages will load inside the iframes. (Meaning it wont load the master-page)

Kinda like my iframe will be the contentplaceholder or maybe the contentplaceholder will be inside it?

Any Ideas?

Cyrillus answered 16/2, 2011 at 10:10 Comment(1)
@Rohit - why are you performing these pointless edits?Osswald
P
30

try this

<iframe name="myIframe" id="myIframe" width="400px" height="400px" runat="server"></iframe>

Expose this iframe in the master page's codebehind:

public HtmlControl iframe
{
get
{
return this.myIframe;
}
}

Add the MasterType directive for the content page to strongly typed Master Page.

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits=_Default" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/MasterPage.master" %>

In code behind

protected void Page_Load(object sender, EventArgs e)
{
this.Master.iframe.Attributes.Add("src", "some.aspx");
}
Phillis answered 16/2, 2011 at 10:16 Comment(3)
Note that this will produce an HTML5 Validation warning, because the src attribute is required.Operation
@Operation would it suffice to provide src="#" in the .aspx page (prior to manipulating it in the code behind)? Would that prevent the HTML5 validation warning?Bichloride
I would think so.Operation
I
11

Another option is to use placeholders.

Html:

<body>
   <div id="root">
      <asp:PlaceHolder ID="iframeDiv" runat="server"/>
   </div>
</body>

C#:

iframeDiv.Controls.Add(new LiteralControl("<iframe src=\"" + whatever.com + "\"></iframe><br />"));
Icono answered 8/11, 2012 at 20:53 Comment(0)
M
10

How about:

<asp:HtmlIframe ID="yourIframe" runat="server" />

Is supported since .Net Framework 4.5

If you have Problems using this control, you might take a look here.

Mezzorelievo answered 20/11, 2014 at 14:0 Comment(0)
O
8

You can think of an iframe as an embedded browser window that you can put on an HTML page to show another URL inside it. This URL can be totally distinct from your web site/app.

You can put an iframe in any HTML page, so you could put one inside a contentplaceholder in a webform that has a Masterpage and it will appear with whatever URL you load into it (via Javascript, or C# if you turn your iframe into a server-side control (runat='server') on the final HTML page that your webform produces when requested.

And you can load a URL into your iframe that is a .aspx page.

But - iframes have nothing to do with the ASP.net mechanism. They are HTML elements that can be made to run server-side, but they are essentially 'dumb' and unmanaged/unconnected to the ASP.Net mechanisms - don't confuse a Contentplaceholder with an iframe.

Incidentally, the use of iframes is still contentious - do you really need to use one? Can you afford the negative trade-offs associated with them e.g. lack of navigation history ...?

Osswald answered 16/2, 2011 at 10:17 Comment(1)
+1: Just to clarify one thing for the OP. If a page references a master page, then the master page will load regardless of how the page is accessed. The only ways to keep the master page from executing is to 1) completely remove the reference to the master, 2) change the reference to a master that doesn't have all the same code behind at design time, or 3) change the master reference at runtime in the page preinit method to some other master.Farrow

© 2022 - 2024 — McMap. All rights reserved.