How to change page's title dynamically

Discussion in 'ASP .Net' started by Guogang, Jan 21, 2004.

  1. Guogang

    Guogang Guest

    Hi, is there a way to change the page's title in code behind (C#)?

    I want to change this part:
    <TITLE> Can be changed dynamically. </TITLE>

    Guogang, Jan 21, 2004
    1. Advertisements

  2. Guogang

    Laidbak Guest

    Hi, is there a way to change the page's title in code behind (C#)?

    Make the text of the title a label.
    In your code behind set the label's text.
    Laidbak, Jan 21, 2004
    1. Advertisements

  3. Guogang

    Guogang Guest

    Well, it works. Never thought I can do this. :)

    Only thing I want to change is that I need to use "Literal" instead of
    "Label", otherwise, the title will have <span> around it.

    Make the text of the title a label.
    In your code behind set the label's text.
    Guogang, Jan 21, 2004
  4. Guogang

    Showjumper Guest

    Give your title tag an id and runat attribute. then declare it in code
    behind And then you can retrive data from a dataset and set the inner text
    property equal to the text or whatever.

    <title id="PageTitle" runat="server"></title>

    Protected WithEvents PageTitle As HtmlGenericControl

    PageTitle.InnerText = ds.Tables(0).Rows(0).Item("ArticleTitle").ToString
    Showjumper, Jan 21, 2004
  5. Guogang

    gerry Guest

    this works , but VS has the annoying habit of removing the runat="server"
    from <TITLE> tags when it feels the urge.
    gerry, Jan 22, 2004
  6. Gerry,

    The problem with what you are trying to do is that ASP.NET treats everything
    from the first character of the output (usually "<!DOCTYP..." for VS.NET
    created WebForms) to the first control as a LiteralControl. this means that
    the title is within this LiteralControl as literal text to be spit out
    (without further processing) at Render-time. This is very efficient, but
    puts the title out of reach for the base implementation of

    The only sure-fire way to completely control this is by creating a class
    that derives from System.Web.UI.Page and then adding private variable and an
    accessor for a property named "Title".

    Then, override the Render method. In that method, you'll need to find the
    <title> control in the text of that first literal control and change what's
    between <title> adn </title> to your internal title variable.

    You can view the code (and description) here:
    Michael Earls, Jan 23, 2004
  7. Guogang

    gerry Guest

    Thanks for the info michael, I will incorporate this in my base page.

    However, imho, this is still a bug in VS in that it is 'randomly' removing
    things from my html.
    The runat="server" method works just fine until VS removes it - I haven't
    pinned down exactly when it is removed.
    gerry, Jan 24, 2004
  8. Guogang

    Ujjwal Singh Roy

    Sep 6, 2013
    Likes Received:
    Very Simple

    Copy And Paste it to page_load

    var page = (Page)HttpContext.Current.Handler;
    page.Title = "someTitle";

    Ujjwal Singh Roy, Sep 6, 2013
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.