how to change PageTitle from PartialView in asp.net mvc - c#

I want to change Page title from partial view. But following error shown
Using the Title property of Page requires a header control on the page. (e.g. <head runat="server" />).
My master page head section is here
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
</title>
</head>
My Default page is here
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%=ViewData["pagetitle"] %>
</asp:Content>
ViewData["pagetitle"] returns current page title for example Home, About, News List, News Detail. But i want to change current news information title instead of News Detail string. News Detail page contains partial view. Partial view know which news shown.
Please help

Not really sure how you have your files structured but if you have an entities folder in which you pull information from for your view page then in the aspx page you can do something like this:
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%:projectName.Resources.Entities.FolderName.FileName%>
</asp:Content>

Because the title is rendered as part of the head section, there is no easy way for a partial view to set the page title directly.
But your controller (which fetches the news item to display) will know what the title should be, and can add the "pagetitle" item to the ViewData collection, which you can then set either in your master layout directly or in a page-specific content region as you are doing above.
Hint: I'd recommend creating a static class called ViewDataKeys with static string properties (or public const fields) used as indexers into the ViewData collection. This helps avoid duplicates, spelling mistakes and case-sensitivity errors in working with the string-based dictionary from multiple sources.

Related

Contentholder as reference to web form in asp.net

I have got a master page with three content holders. Say; Header, Menu and Content.
I can use Iframes or regular frames, but is there a way to specify the three content holders as follows and redirect them to three different web forms?
The reason I ask this, is because I do not want to specify my menu over and over again for every single web form.
<asp:Content ID="Content1" ContentPlaceHolderID="Header" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Menu" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Content" runat="server">
</asp:Content>
You've missed the point of a master page. A master page should contain the markup/code that is common to the content pages or nested master pages that reference it. Put the menu code into the master page.
<!-- Menu code here -->
<ul class="menu">
<li>Home"</li>
<li>Contact"</li>
</ul>
!-- End menu code -->
<asp:ContentPlaceHolder ID="MenuPlaceHolder" runat="server">
<!-- On the content page, your page specific menu code would go in the <asp:Content> that references this -->
</asp:ContentPlaceHolder >
As an alternative to (or in conjunction with) master pages, you can use user controls. You place your menu code in the user control and embed it in master pages or content pages. But if you always want it in the same place, then a master page makes more sense because of the Don't Repeat Yourself principle.
There is absolutely no reason to use an iframe for this.

<asp:Content> does not correspond with <asp:ContentPlaceholder>

My ASP.NET page was not showing the controls. It just shows that master page error
The page has one or more <asp:content> controls that do not correspond
with <asp:ContentPlaceHolder controls in the Master Page
Try setting
<title></title>
instead of
<title />
Remove comment (<%-- --%>) in the Master file, this worked for me.
All your Content controls have to reference existing ContentPlaceHolders on master page
For instance, if you have on your page
<asp:Content ContentPlaceHolderID="Header" ID="Title" runat="server">
You must have something like this on your master page
<asp:ContentPlaceHolder ID="Header" runat="server" />
ContentPlaceHolderID is the property that must match against any ContentPlaceHolder ID on master page.
The problem is that you must have a syntax error in the master.page's markup :
Ex) %²> in stead of %>
If you validate your markup, I'm sure thar this will solve your problem
PI. I has the same error & it works for me ;)

Retrieving page title from master page

To manage the page title on my pages, I have a master page with a ContentPlaceHolder in the header.
<head runat="server">
<asp:ContentPlaceHolder runat="server" ID="headContent">
</asp:ContentPlaceHolder>
</head>
On each of my pages, I add the meta tags and page title as follow:
<asp:content id="Header" contentplaceholderid="headContent" runat="server">
<meta name="keywords" content="keyword1, keyword2" />
<meta name="description" content="page description" />
<%Page.Title = "My page title";%>
</asp:content>
I cannot modify the code on the pages by placing the Page.Title in the OnInit method of the page.
I need access to the page title in the codebehind of the master page, but I always get an empty title when I use Page.Title.
By using <%Page.Title = "My page title";%> you implicitly tell the ASP.NET to execute this embedded code block during the page's render phase.
What does it mean? You won't be able to get this value before the page's render phase. Assuming you're trying to get this value a little bit earlier than during the render. That's why you get the empty string.
The workaround could be in setting your Title property of the <%# Page directive in the beginning of your page e.g.:
<%# Page Title="My Title Goes Here" Language="C#" ... %>
By setting this you'll be able to access the Page.Title property from your master page a little bit earlier than the page's rendering occurs.
Just use
<title>My page title</title>

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.

Master pages -- Getting calling page name

Can someone tell me how to get the name of the child page being called in a Master page scenario. For example, if I have the following masterpage:
<%# Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<asp:ContentPlaceHolder ID="cphMainContent" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
And I create a page called Test.aspx which inherits this masterpage, how can I, from the masterpage, know that Test.aspx has been requested (since this masterpage can be inherited by more then 1 aspx page)?
Going with your comments, I see a couple of options:
1) In your login page, on a non-postback, check the Referrer - this will be the page that sent you to the login page (Request.UrlReferrer), store that in session for the postback of their login details, and then send the user back.
2) Use the standard features of ASP.NET to handle login/redirections (which basically use the same system as 1).
This is a similar question to what you're asking, but I would pay attention to the accepted answer, trying to figure out the child page from the master page is probably not a good design idea.
I would notify in the other direction. Add properties to your master page then set them from the content pages.
In your master:
public partial class PortalMaster : System.Web.UI.MasterPage
{
public string PageSection { get; set; }
}
In your .aspx add this directive:
<%# MasterType VirtualPath="PortalMaster.master" %>
Then in your .aspx.cs set the property value like so:
Master.PageSection = "about";
Just use the "Page" member of the masterpage
From the codebehind in the MasterPage, the Page.AppRelativeVirtualPath property will tell you the path.

Categories