简体   繁体   English

如何在页面加载时动态更改aspx页面的标题

[英]how to change title of aspx page dynamically on page load

I had set of ASPX pages in which each page had different titles, but I want to put default title for pages which don't have a title.我有一组 ASPX 页面,其中每个页面都有不同的标题,但我想为没有标题的页面设置默认标题。 The default title must be configurable.默认标题必须是可配置的。

If this is classic ASP.NET (not MVC) and you are using MasterPage then you can set default title in Page_Load event in MasterPage :如果这是经典的 ASP.NET(不是 MVC)并且您使用的是MasterPage那么您可以在MasterPage Page_Load事件中设置默认标题:

protected void Page_Load(object sender, EventArgs e)
{
      if (string.IsNullOrEmpty(Page.Title))
      {
           Page.Title = ConfigurationManager.AppSettings["DefaultTitle"];  //title saved in web.config
      }
}

You can do this:你可以这样做:

Set the aspx header something like this像这样设置 aspx 标头

<HEAD> 
<TITLE ID=CaptionHere RUNAT="server"></TITLE> 
</HEAD> 

And in code behind put this inside the page load event:在后面的代码中把它放在页面加载事件中:

if(!IsPostBack)
{
  myCaption.InnerHtml = "Hope this works!"
}

I hope this will help you我希望这能帮到您

I had a similar problem and none of these solutions worked well for me.我遇到了类似的问题,这些解决方案都不适合我。 The problem stems from the order control events fire for a page.问题源于页面的顺序控制事件触发。 In my case, I had some code that needed to be in the Page_load event (this was because that is the first event where we have a Request object to work with).在我的例子中,我有一些代码需要在 Page_load 事件中(这是因为这是我们有一个 Request 对象可以使用的第一个事件)。 That code also needed to run before the title could be set.该代码还需要在设置标题之前运行。 Other pages in my site were able to simply set the desired Title in the page Ctor but because this page needed to interrogate the response object for information first, it was a problem.我站点中的其他页面能够简单地在页面 Ctor 中设置所需的标题,但是因为该页面需要首先询问响应对象的信息,所以这是一个问题。 The problem with this is that the master page has already created the page header section by the time we get to the Page_load event and I didn't want junk in my Master page that was only required for a single page on my site.这样做的问题是母版页在我们到达 Page_load 事件时已经创建了页眉部分,我不希望母版页中只有我网站上的单个页面需要的垃圾。 My simple hack to overcome this issue was to insert a bit of javascript inline in the content portion of the page:我克服这个问题的简单技巧是在页面的内容部分插入一些内联的 javascript:

<asp:Content ID=BodyContent ContentPlaceHolderID=MainContent RunAt=Server>
    <script type="text/javascript">
        document.title='<%=Title%>';
    </script>

    ... the rest of the content page goes here ...

</asp:Content>

With this in place, you are free to set the Title in the Page_Load event and it'll be set as soon as this line of code has downloaded.有了这个,你就可以在 Page_Load 事件中自由设置标题,一旦这行代码下载,它就会被设置。 Of course, my site already has a JS requirement so if you're trying to avoid that then this is not going to work for you.当然,我的网站已经有一个 JS 要求,所以如果你想避免这种情况,那么这对你来说是行不通的。

protected void Page_Load(object sender, EventArgs e)
{
     Page.Title = title();
}
private string title()
{

    SqlConnection con = new SqlConnection(cs);
    string cmdstr = "select * from title where id = 2";
    SqlCommand cmd = new SqlCommand(cmdstr, con);
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    con.Open();
    da.Fill(dt);
    con.Close();
    if (dt.Rows.Count > 0)
    {
        string title = dt.Rows[0]["title"].ToString();
    }
    return title;
}

This is helpful这很有帮助

在后面的母版页代码中,您可以设置[this.Title = "Whatever";]或者您也可以在 HTML 中指定默认标题。

I wanted to change the title of the home page same as you.我想更改与您相同的主页标题。 In the main file in your hosting, which contains all the home page code, the name may differ on different websites.在包含所有主页代码的主机中的主文件中,不同网站的名称可能不同。 Open it and then look for the title tag inside the header tag.打开它,然后在标题标签中查找标题标签。 Which is as follows and you can change it to your liking.如下所示,您可以根据自己的喜好进行更改。 If you want to write something under the title, like what I did on page Irpolymer , you have to add a如果你想在标题下写一些东西,就像我在Irpolymer页面上所做的那样,你必须添加一个

tag and write the text you want, then you can style it or change its size and then save.标记并编写您想要的文本,然后您可以设置样式或更改其大小,然后保存。 Consider taking a backup of your service before making changes so that you can restore your site in the event of a problem.考虑在进行更改之前备份您的服务,以便在出现问题时可以恢复您的站点。

 <head runat="server">
 <title>Untitled Page</title>
 <p>something you want</p>
 <asp:ContentPlaceHolder id="head" runat="server">
 </asp:ContentPlaceHolder>
 </head>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM