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

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

Related

How do you pass data between pages where the source data is in IN an asp:Content block?

So I've noticed that PreviousPage and Request.Form don't work if my SOURCE page has the TextBox's and such within an asp:Content block (master pages)
Is there a workaround or am I not understanding something?
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" MasterPageFile="Menu.master" %>
<asp:Content ID="pageHead" ContentPlaceHolderID="pageHead" Runat="Server">
<title>Whatever</title>
</asp:Content>
<asp:Content ID="pageContent" ContentPlaceHolderID="pageContent" Runat="Server" >
<asp:TextBox runat="server" ID="txtTester" Text="YES"></asp:TextBox>
<asp:Button runat=server ID="btnTest" PostBackUrl="~/Process.aspx" />
</asp:Content>
Target Codebehind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ltlDebug.Text = Request.Form["txtTester"];
//Doesn't get the var, only if I remove it from master page model in source page
}
}
This is similar to How to read input value from the Request.Form collection by input name.
A control's Form key is the unique id of the control. When you nest your control inside of a containing control (such as Content) that implements INamingContainer, ASP.NET will use the nesting control's name to qualify the nested control's id (and name).
So the Form key will be a concatenation of the Content and the TextBox control's IDs - something like "pageContent$txtTester". You can look at the page's generated markup and use whatever the text box's id value.
You can use response.redirect("yoursite.aspx?parameter1=value"); for that.

How can i add iframe in my master page

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.

ASP.NET MVC - How To Design Dynamic H1/Title Scheme?

If i have an ASP.NET MVC 2 Web Application with the following Views:
Main.aspx
Page1.aspx
Page2.aspx
And all Views inherit from a MasterView called Site.master,
I want to be able to have a default Title/H1 for the page, which can be overriden in the deriving Views.
E.g Main.aspx would have "MySite - xxx", Page1.aspx would have "MySite - Page 1", Page2.aspx would have "MySite - Page2".
And if i choose not to set the Title/H1 in a new derived view, the master Title/H1 would be displayed.
With WebForms, i would accomplish this in the following way:
Make the Title/H1 tags on the master runat="server"
Expose protected properties on the master code-behind
On Page_Load of the derived pages code-behind, set the property (Master.Title = "Page 1").
On Page_PreRender of the master code-behind, set the Title/H1 tags to "My Site - " + Title;
How can we accomplish this with ASP.NET MVC?
I could do this in the master:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
And then set it in the view:
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
MySite - Page 1
</asp:Content>
But i wanted to be able to only specify "Page 1" in the View, and the title gets magically changed to "MySite - Page 1". Know what i mean? The "MySite - " part of the title needs to be the template for the title.
I'm probably missing something obvious here. :)
With a quick search I found this:
http://haacked.com/archive/2009/04/03/tipjar-title-tags-and-master-pages.aspx
explains why
<title>MySite<asp:content..../></title>
doesn't work
This is how I usually do it:
<title>MySite - <%: Page.Title ?? "Default title" %></title>
in your MasterPage.
Then, you can define the Title property on a content page like this:
<%# Page Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage"
Title="Page 1"
%>
Edit:
Well, you might want to see this SO question: ASP.NET MVC - View with master page, how to set title?.
Is much easier as you are describing it.
Just add a content placeholder to your master page
<title>
My Site - <asp:ContentPlaceHolder ID="PageTitle" runat="server" />
</title>
Then in your Content Page use it as
<asp:Content ID="Content3" ContentPlaceHolderID="PageTitle" runat="server">
My Page
</asp:Content>
This way will work but you have to use an HTML HEAD tag, not a server control. So simply remove runat="server" from your HEAD.

MVC2 C# - Why I cannot assign a Model.ImageUrl to a img src attribute in a UserControl?

I'm starting with MVC 2 in Visual Studio 2010. The thing is I'm working with a MasterPage where I'm rendering an action which returns a UserControl:
Everything works well, I mean the user control has a lot of other labels and it's rendering other properties... So the problem has nothing to do with anything apart the img tag and the src attribute:
Site.master:
<div class="Test">
<% Html.RenderAction("HeaderDetails", "User"); %>
</div>
HeaderDetails.ascx:
<img src="<%= Model.ImageUrl %>" />
For my surprise I cannot use the Model inside that attribute in a usercontrol, I have the same for a Page.aspx that is working.
The same happens with ViewData["ImageUrl"], I just don't have even intellicense in that attribute. It's like: I cannot do that.
Does anyone know why is that?, or how should I do it?
I kind of got lost towards the end of your question, but in regards to the strongly typed Model in the user control, you'll need your page, masterpage and user control to all inherit with the same type arguments
page:
<%# Page Language="C#" MasterPageFile="Your.Master" Inherits="System.Web.Mvc.ViewPage<YourModelType>" %>
master:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<YourModelType>" %>
control:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<YourModelType>" %>
The type arg of the control and master can be base types of YourModelType, but obviously will need to have the ImageUrl property
Well, I have solved it by creating a Custom HtmlHelper using Extension Methods going through this link.
The only matter is that I couldn't use the namespace MyMVCApp.Helpers, I had to use the System.Web.MVC one, so I'm not sure that would be the best way to do it.
If you have some input on that I will appreciate it.
So, I was missing the Import to MyMVCApp.Helpers inside the user control :D

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>

Categories