Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Set Page Title from Code Behind

Tags:

c#

asp.net

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.

like image 382
user2029541 Avatar asked Jan 16 '14 15:01

user2029541


People also ask

What is ASP content tag?

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.


4 Answers

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

like image 171
nestedloop Avatar answered Sep 27 '22 19:09

nestedloop


The Page has a Title property:

protected void Page_Load(object sender, EventArgs e) {     this.Title = "Title"; } 
like image 43
Tim Schmelter Avatar answered Sep 27 '22 19:09

Tim Schmelter


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";
        }
    }
    
like image 23
Enoch Olaoye Avatar answered Sep 27 '22 21:09

Enoch Olaoye


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"));
    }
like image 41
Alec Effrat Avatar answered Sep 27 '22 21:09

Alec Effrat