I am creating data driven pages using ASP.NET C# and want to dynamically set the page title using code behind
<%@ Page Title="" Language="C#" MasterPageFile="~/FLMaster.master" AutoEventWireup="true" CodeFile="legal-expenses-insurance-news-item.aspx.cs" Inherits="legal_expenses_insurance_news_legal_expenses_insurance_news_item" %>
I have tried using the separate title tags lower down in the page but this didn't work either. Can anyone advise how to do this.
asp:Content ID="HeaderContent" is a content region. Anything within that tag will get embedded in the associated ContentPlaceHolder in the master page when it is generated. head is a standard html markup, indicating the page head elements.
using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace MyApplication { public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { this.Title = "Title of my page"; } } }
You can modify the Page title like above from your aspx.cs (the code behind file).
If you want to do this directly in your .aspx file, try this:
<% this.Title = "Some Title" %>
This works if you correctly set your Language = "C#"
in your @Page
directive, which I see you did.
Page class reference from MSDN
The Page
has a Title
property:
protected void Page_Load(object sender, EventArgs e) { this.Title = "Title"; }
You should remove the Title="" from the aspx page. It will override the Title set in your code-behind
<%@ Page Language="C#" MasterPageFile="~/FLMaster.master" AutoEventWireup="true" CodeFile="legal-expenses-insurance-news-item.aspx.cs" Inherits="legal_expenses_insurance_news_legal_expenses_insurance_news_item" %>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.Title = "Title";
}
}
I know that this is an old thread, but I found that Page.Title couldn't always be overriden, but Page.Header.Title could (mostly)... so my solution for dynamic title tags from the Master codebehind is:
if (Page.Header != null)
{
if (Page.Header.Title == null || !Page.Header.Title.Contains("COMPANYNAME"))
{
var otitle = Page.Header.Title;
if (otitle == null || otitle.Length==0) {
var textinfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo;
otitle = textinfo.ToTitleCase(this.Parent.GetType().Name.Replace("_"," ").Replace("aspx",""));
}
Page.Header.Title = "COMPANYNAME" + " - " + otitle;
}
Page.Header.Title = Page.Header.Title.Replace("COMPANYNAME", Auth.getSetting("companyName"));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With