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:
    <HEAD>
    <TITLE> Can be changed dynamically. </TITLE>
    </HEAD>

    Thanks,
    Guogang
    Guogang, Jan 21, 2004
    #1
    1. Advertising

  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.

    --
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Wil Moore III, MCP Site : www.quicksitedesign.com?em
    Application Developer Site : www.digitallysmooth.com?em
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Laidbak, Jan 21, 2004
    #2
    1. Advertising

  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.

    "Laidbak" <> wrote in message
    news:...
    >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.

    --
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Wil Moore III, MCP Site : www.quicksitedesign.com?em
    Application Developer Site : www.digitallysmooth.com?em
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Guogang, Jan 21, 2004
    #3
  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

    "Guogang" <nospam@no_such_domain.com> wrote in message
    news:OcjP3$...
    > Hi, is there a way to change the page's title in code behind (C#)?
    >
    > I want to change this part:
    > <HEAD>
    > <TITLE> Can be changed dynamically. </TITLE>
    > </HEAD>
    >
    > Thanks,
    > Guogang
    >
    >
    Showjumper, Jan 21, 2004
    #4
  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.


    "Showjumper" <> wrote in message
    news:%23O$...
    > 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
    >
    > "Guogang" <nospam@no_such_domain.com> wrote in message
    > news:OcjP3$...
    > > Hi, is there a way to change the page's title in code behind (C#)?
    > >
    > > I want to change this part:
    > > <HEAD>
    > > <TITLE> Can be changed dynamically. </TITLE>
    > > </HEAD>
    > >
    > > Thanks,
    > > Guogang
    > >
    > >

    >
    >
    gerry, Jan 22, 2004
    #5
  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
    System.Web.UI.Page.

    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:

    http://www.cerkit.com/cerkitBlog/PermaLink.aspx?guid=54fe3c55-91ca-4318-856d-c1e62963db9b
    Michael Earls, Jan 23, 2004
    #6
  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.




    "Michael Earls" <> wrote in message
    news:%...
    > 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
    > System.Web.UI.Page.
    >
    > 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:
    >
    >

    http://www.cerkit.com/cerkitBlog/PermaLink.aspx?guid=54fe3c55-91ca-4318-856d-c1e62963db9b
    >
    >
    gerry, Jan 24, 2004
    #7
  8. Guogang

    Ujjwal Singh Roy

    Joined:
    Sep 6, 2013
    Messages:
    1
    Location:
    kolkata
    Very Simple

    Copy And Paste it to page_load


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

    :lol:
    Ujjwal Singh Roy, Sep 6, 2013
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    1
    Views:
    447
    Steve C. Orr, MCSD
    Aug 10, 2003
  2. Replies:
    0
    Views:
    514
  3. Replies:
    1
    Views:
    532
    David
    Apr 24, 2007
  4. soonic
    Replies:
    11
    Views:
    1,768
    soonic
    Jul 27, 2011
  5. Russell
    Replies:
    2
    Views:
    276
    Russell
    Sep 13, 2004
Loading...

Share This Page