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?

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
Or Betzalel
  • 2,427
  • 11
  • 47
  • 70

4 Answers4

31

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");
}
John C
  • 3,052
  • 3
  • 34
  • 47
santosh singh
  • 27,666
  • 26
  • 83
  • 129
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 />"));
RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
owen gerig
  • 6,165
  • 6
  • 52
  • 91
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.

Alex
  • 14,104
  • 11
  • 54
  • 77
Philipp Hofmann
  • 3,388
  • 26
  • 32
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 ...?

immutabl
  • 6,857
  • 13
  • 45
  • 76
  • 2
    +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. – NotMe Feb 16 '11 at 17:13