[英]ASP.NET Set Page Title from Code Behind
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.
任何人都可以建议如何做到这一点。
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
指令中正确设置了Language = "C#"
,这就行了,我看到了。
The Page
has a Title
property : Page
有一个Title
属性 :
protected void Page_Load(object sender, EventArgs e)
{
this.Title = "Title";
}
You should remove the Title="" from the aspx page. 您应该从aspx页面中删除Title =“”。 It will override the Title set in your code-behind
它将覆盖代码隐藏中的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";
}
}
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"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.