How can i add iframe in my master page - c#

I want to add iframe in my master page. suppose i ll refer this master page to other aspx page, the content of the contentplaceholder always display in iframe . Please help me to do this ..
This is my master page contenplace holder code:
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
please help me ...

Based on your comment to VinayC, you don't want a Master page. You want a container page, old school HTML or another aspx, that will hold your iFrame. As VinayC pointed out, a master page is just a template, not an actual page that gets served. It's a layout template, nothing more.
If we're misunderstanding your intent and you need to inject an iFrame into a page, here's a bit of javascript that will do that:
function InsertIFrame(path) {
var iframe = document.createElement('iframe');
iframe.src = path;
document.body.appendChild(iframe);
}

Probably MasterPage/ContentPage does not work the way you are thinking. ASP.NET simply merges the content of both to produce the output. Master-page is just a layout that will be used while displaying the content page - the master page itself is not a navigable content.
From what you are describing, you probably don't need master page. What you need is a normal page that will have your basic layout along with an iframe whose source you can set to start-page. Once loaded, all the navigation will happen in the iframe giving you what you want.

Related

ASP.NET C# how to Ignore certain styling in MasterPage

So I created a master page which serves as the Header and Footer template for every other template, the problem however, in one of the form, I put all the HTML that I wanted, CSS and JS when I load it theres some problem with the result. I figure out the problem is caused by this code
<form id="form1" runat="server">
If I remove that code, then the output will be what I want, the problem is that I dont want to remove that code in the MasterPage, is there a way to set that the Web form that follow that masterPage will ignore the
<form id="form1" runat="server">
Thanks and have a great day

Using iFrames In ASP.NET

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?
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");
}
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 />"));
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.
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 ...?

ASP.NET how to set text on a master page?

I have a master page called MasterPage.master which has a <fieldset> with a <legend> tag. Something like this:
<fieldset id="NewTrade" runat="server">
<legend runat="server" class="legend"><%= this.BodyTitle %></legend>
<asp:ContentPlaceHolder id="contentMain" runat="server" />
</fieldset>
The masterpage.master file inherits SiteMaster from SiteMaster.cs. SiteMaster has a public field called BodyTitle. I want to set the BodyTitle when a normal page loads but I'm not sure how to do that. Basically all I want to do is have a legend surrounding my master content and then set the legend text at page load time for each page. What's the best way to do that?
So, in say Default.aspx I want to do something like this in Page_Load:
BodyTitle.Text = "Home"
Thanks
(this.Master as SiteMaster).BodyTitle.Text = "Home";
This casts the Master page property of your Page to the base class SiteMaster. You can also cast it directly to the MasterPage class (from your MasterPage.master), but if you are going to do this then #Greg's answer is better, although they will both work. Just depends on your requirements. Setting the MasterType property of the aspx page is a great solution, but if you are doing dynamic switching of your master page or would like to be more flexible, then the above solution would fit better.
You would use the Master property of the Page object, and cast it to your SiteMaster class.
((SiteMaster)this.Master).BodyTitle = "Home";
You can put this at the top of your Content page:
<%# MasterType VirtualPath="~/masterpage.master" %>
That will automatically cause the Master property of your page to be of the type of your master class, so you can then access the property without casting.
Save the text in a variable.
public string Heading { get; set; }
Then add the value to this variable from the other page;
(this.Master as Site1).Heading = "Hello";

Disable feature in Master Page from Content Page

I have a Master Page for all my ASP.NET pages. It works fine for 95% of pages... Except that it also includes unwanted attributes that are included in the other 5% of pages. This causes application problems. Is it possible for Content Pages to somehow select to enable/disable a feature available in the master page? One option I can think of is for the Master Page to look at the page name and then decide what to do, but that's a bit clunky in the long run...
You can add a MasterType directive in your content page so you will have access to the master page class... and from there you can implement to enable disable features of your master page...
<%# MasterType virtualpath="~/MyMaster.master"%>
and in your code behind you will have access to the Master property as a typed class...
In your master page provide a content placeholder:
<asp:ContentPlaceHolder ID="foo" runat="server">
<div>Some default content</div>
</asp:ContentPlaceHolder>
In the 5% of the pages that don't need the default behavior override this placeholder with an empty string:
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="foo" />
The other 95% of the pages will get the common behavior.
You can access the master page directly by getting at the page and casting it to the type of your master page as below. This will couple you to the master page but the only other option I can think of is to create a key in session.
(MyMasterPage)Page.Master
Assuming that these features are represented in the asp markup of the master page, you could wrap it within a ContentPlaceHolderControl:
<asp:ContentPlaceHolder ID="OptionalContent" runat="server">
*** This is my optional content ***
</asp:ContentPlaceHolder>
In the 95% of pages where this content works, you can just not include a Content control for "OptionalContent" on your page. For the 5% where it doesn't work, you can include an empty Content control for "OptionalContent":
<asp:Content ContentPlaceHolderID="OptionalContent" runat="server"></asp:Content>

SharePoint Master Page modified SPSWC:SearchBoxEx not working correctly on each site

I have created a custom master page and have the follwoing code on the page:
<asp:ContentPlaceHolder id="PlaceHolderSearchArea" runat="server">
<SPSWC:SearchBoxEx id="SearchBox"
RegisterStyles="false"
TextBeforeDropDown=""
TextBeforeTextBox="Search"
TextBoxWidth="120"
GoImageUrl="/_layouts/images/gosearch.png"
GoImageUrlRTL="/_layouts/images/gosearch.png"
UseSiteDefaults="true"
DropDownMode = "HideScopeDD"
SuppressWebPartChrome="true"
runat="server"
WebPart="true"
__WebPartId="{07E563F9-A259-4829-920F-03829BBC14D1}"
GoImageActiveUrl="/_layouts/images/gosearch.png"
GoImageActiveUrlRTL="/_layouts/images/gosearch.png"/>
</asp:ContentPlaceHolder>
On one site this code works correctly and on another it does not work and the default search box apears. I can not work out why this is happening. Any ideas?
The content in the contentplaceholder "PlaceHolderSearchArea" can be overwritten by a <asp:Content /> element in a page layout. So check the page layouts you are using for the existance of an <asp:Content/> with the placeholder id "PlaceHolderSearchArea".

Categories