ASP.NET从代码背后设置页面标题

时间:2022-08-25 23:02:21

I am creating data driven pages using ASP.NET C# and want to dynamically set the page title using code behind

我正在使用ASP.NET C#创建数据驱动页面,并希望使用后面的代码动态设置页面标题

<%@ 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.

我尝试在页面中使用较低的单独标题标签,但这也不起作用。任何人都可以建议如何做到这一点。

4 个解决方案

#1


25  

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).

您可以从aspx.cs(文件后面的代码)修改上面的页面标题。

If you want to do this directly in your .aspx file, try this:

如果要直接在.aspx文件中执行此操作,请尝试以下操作:

<% this.Title = "Some Title" %>

This works if you correctly set your Language = "C#" in your @Page directive, which I see you did.

如果你在@Page指令中正确设置了你的语言=“C#”,这就行了,我知道你做了。

Page class reference from MSDN

来自MSDN的页面类引用

#2


8  

The Page has a Title property:

该页面有一个Title属性:

protected void Page_Load(object sender, EventArgs e)
{
    this.Title = "Title";
}

#3


4  

You should remove the Title="" from the aspx page. It will override the Title set in your code-behind

您应该从aspx页面中删除Title =“”。它将覆盖代码隐藏中的标题集

<%@ 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";
        }
    }
    

#4


0  

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:

我知道这是一个旧线程,但我发现Page.Title并不总是被覆盖,但是Page.Header.Title可以(大部分)......所以我对Master代码隐藏的动态标题标签的解决方案是:

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

#1


25  

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).

您可以从aspx.cs(文件后面的代码)修改上面的页面标题。

If you want to do this directly in your .aspx file, try this:

如果要直接在.aspx文件中执行此操作,请尝试以下操作:

<% this.Title = "Some Title" %>

This works if you correctly set your Language = "C#" in your @Page directive, which I see you did.

如果你在@Page指令中正确设置了你的语言=“C#”,这就行了,我知道你做了。

Page class reference from MSDN

来自MSDN的页面类引用

#2


8  

The Page has a Title property:

该页面有一个Title属性:

protected void Page_Load(object sender, EventArgs e)
{
    this.Title = "Title";
}

#3


4  

You should remove the Title="" from the aspx page. It will override the Title set in your code-behind

您应该从aspx页面中删除Title =“”。它将覆盖代码隐藏中的标题集

<%@ 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";
        }
    }
    

#4


0  

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:

我知道这是一个旧线程,但我发现Page.Title并不总是被覆盖,但是Page.Header.Title可以(大部分)......所以我对Master代码隐藏的动态标题标签的解决方案是:

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