Inserting Page Title in HTML Automatically

Discussion in 'ASP .Net' started by Jonathan Wood, Jan 2, 2008.

  1. I'd like to have a page's title appear within the page (ideally, by
    inserting something in a master page).

    I tried the following:

    xxx <%# Page.Title %> xxx

    I tried this in both a content page and a master page. It compiles without
    error but, in both cases, the two "xxx" showed up with only a space between.

    Is this possible? I would really prefer not to use a label that is set from
    code.

    Thanks.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com
     
    Jonathan Wood, Jan 2, 2008
    #1
    1. Advertising

  2. "Jonathan Wood" <> wrote in message
    news:%...

    > Is this possible?


    > xxx <%# Page.Title %> xxx


    xxx <%= Page.Title %> xxx

    # is databinding syntax...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Jan 2, 2008
    #2
    1. Advertising

  3. Playing around with this, I came up with:

    <h1><% Response.Write(Page.Title); %></h1>

    Which seems to work.

    Can anyone tell me if this is the most efficient way? In particular, I was
    hoping for something the compiler could actually replace the text with the
    name of the page, so the code wouldn't be necessary at runtime. But my hunch
    is that this would involve that bit of code running everytime the page
    loads.

    Thanks.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    "Jonathan Wood" <> wrote in message
    news:%...
    > I'd like to have a page's title appear within the page (ideally, by
    > inserting something in a master page).
    >
    > I tried the following:
    >
    > xxx <%# Page.Title %> xxx
    >
    > I tried this in both a content page and a master page. It compiles without
    > error but, in both cases, the two "xxx" showed up with only a space
    > between.
    >
    > Is this possible? I would really prefer not to use a label that is set
    > from code.
    >
    > Thanks.
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
     
    Jonathan Wood, Jan 2, 2008
    #3
  4. Jonathan,
    I'm not sure I completely understand what your goal is here, but the Page
    class in ASP.NET 2.0 has a handy "Title" property, e.g.

    Page_Load( xxxx )
    {
    Page.Title ="This is the Page Title Dood!";
    }
    -- Peter
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    MetaFinder: http://www.blogmetafinder.com


    "Jonathan Wood" wrote:

    > Playing around with this, I came up with:
    >
    > <h1><% Response.Write(Page.Title); %></h1>
    >
    > Which seems to work.
    >
    > Can anyone tell me if this is the most efficient way? In particular, I was
    > hoping for something the compiler could actually replace the text with the
    > name of the page, so the code wouldn't be necessary at runtime. But my hunch
    > is that this would involve that bit of code running everytime the page
    > loads.
    >
    > Thanks.
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
    > "Jonathan Wood" <> wrote in message
    > news:%...
    > > I'd like to have a page's title appear within the page (ideally, by
    > > inserting something in a master page).
    > >
    > > I tried the following:
    > >
    > > xxx <%# Page.Title %> xxx
    > >
    > > I tried this in both a content page and a master page. It compiles without
    > > error but, in both cases, the two "xxx" showed up with only a space
    > > between.
    > >
    > > Is this possible? I would really prefer not to use a label that is set
    > > from code.
    > >
    > > Thanks.
    > >
    > > --
    > > Jonathan Wood
    > > SoftCircuits Programming
    > > http://www.softcircuits.com
    > >

    >
    >
     
    Peter Bromberg [C# MVP], Jan 2, 2008
    #4
  5. Peter,

    > I'm not sure I completely understand what your goal is here, but the Page
    > class in ASP.NET 2.0 has a handy "Title" property, e.g.
    >
    > Page_Load( xxxx )
    > {
    > Page.Title ="This is the Page Title Dood!";
    > }


    I'm not trying to set the title text. I'm trying to insert the title text
    within my HTML text.

    I could create a label and set the label's text from the Load handler. But
    that means that I'll need this little bit of code in every single one of
    dozens and dozens of pages. That's why I said I would really prefer not to
    use a label that is set from code.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com
     
    Jonathan Wood, Jan 2, 2008
    #5
  6. Mark,

    >> Is this possible?

    >
    >> xxx <%# Page.Title %> xxx

    >
    > xxx <%= Page.Title %> xxx
    >
    > # is databinding syntax...


    Makes sense. I was trying to duplicate something I'd seen in a gridview
    template. I had a hard time finding a good reference on the different
    syntaxes and how they are used. (Google won't search on punctuation and the
    few books in front of me do not list <% in the index.)

    Thanks.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com
     
    Jonathan Wood, Jan 2, 2008
    #6
  7. Jonathan Wood

    bruce barker Guest

    the easiest and cleanest is to create your own control. just a couple of
    lines of code:

    public class PageTitle : Label
    {
    protected override void Render(HtmlTextWriter output)
    {
    Text = Page.Title;
    base.Render(output);
    }
    }


    then you can

    <myControls:pageTitle runat="Server">

    anywhere you want the title to render.

    -- bruce (sqlwork.com)


    "Jonathan Wood" wrote:

    > I'd like to have a page's title appear within the page (ideally, by
    > inserting something in a master page).
    >
    > I tried the following:
    >
    > xxx <%# Page.Title %> xxx
    >
    > I tried this in both a content page and a master page. It compiles without
    > error but, in both cases, the two "xxx" showed up with only a space between.
    >
    > Is this possible? I would really prefer not to use a label that is set from
    > code.
    >
    > Thanks.
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
    >
     
    bruce barker, Jan 2, 2008
    #7
  8. I'm not sure if I think that is a better solution, but it is an interesting
    approach.

    Thanks!

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com


    "bruce barker" <> wrote in message
    news:...
    > the easiest and cleanest is to create your own control. just a couple of
    > lines of code:
    >
    > public class PageTitle : Label
    > {
    > protected override void Render(HtmlTextWriter output)
    > {
    > Text = Page.Title;
    > base.Render(output);
    > }
    > }
    >
    >
    > then you can
    >
    > <myControls:pageTitle runat="Server">
    >
    > anywhere you want the title to render.
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "Jonathan Wood" wrote:
    >
    >> I'd like to have a page's title appear within the page (ideally, by
    >> inserting something in a master page).
    >>
    >> I tried the following:
    >>
    >> xxx <%# Page.Title %> xxx
    >>
    >> I tried this in both a content page and a master page. It compiles
    >> without
    >> error but, in both cases, the two "xxx" showed up with only a space
    >> between.
    >>
    >> Is this possible? I would really prefer not to use a label that is set
    >> from
    >> code.
    >>
    >> Thanks.
    >>
    >> --
    >> Jonathan Wood
    >> SoftCircuits Programming
    >> http://www.softcircuits.com
    >>
    >>
     
    Jonathan Wood, Jan 3, 2008
    #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:
    482
    Steve C. Orr, MCSD
    Aug 10, 2003
  2. Replies:
    0
    Views:
    561
  3. Replies:
    1
    Views:
    578
    David
    Apr 24, 2007
  4. soonic
    Replies:
    11
    Views:
    1,827
    soonic
    Jul 27, 2011
  5. Miguel Angel

    inserting image in title of html page

    Miguel Angel, Sep 28, 2004, in forum: Javascript
    Replies:
    4
    Views:
    247
    Miguel Angel
    Sep 30, 2004
Loading...

Share This Page