Controls in the <head> section

Discussion in 'ASP .Net' started by =?Utf-8?B?QXJuZQ==?=, Jan 10, 2006.

  1. I need literal controls in the <head> section of my page. I need to be able
    to load dynamic values in the head section. It works fine in ASP.Net 1.1, but
    Asp.net 2.0 gives a compile error.
    I need the dynamically assigned <base href='??'> and
    <META NAME="Description" CONTENT="??">
    Is there a work around?
    =?Utf-8?B?QXJuZQ==?=, Jan 10, 2006
    #1
    1. Advertising

  2. =?Utf-8?B?QXJuZQ==?=

    Karl Seguin Guest

    Not sure, but check the Page.Header class, it has a bunch of useful
    functionality, including the ability to add controls do it,
    Page.Header.Controls.Add(...)

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Arne" <> wrote in message
    news:...
    >I need literal controls in the <head> section of my page. I need to be able
    > to load dynamic values in the head section. It works fine in ASP.Net 1.1,
    > but
    > Asp.net 2.0 gives a compile error.
    > I need the dynamically assigned <base href='??'> and
    > <META NAME="Description" CONTENT="??">
    > Is there a work around?
    Karl Seguin, Jan 10, 2006
    #2
    1. Advertising

  3. Just off the top of my head (which works), in Page_Load:

    StringBuilder sb = new StringBuilder();
    string someName = "keywords";
    string someKeywords = "blah, doh";
    sb.Append("<meta name=\"");
    sb.Append(someName);
    sb.Append("\" content=\"");
    sb.Append(someKeywords);
    sb.Append("\" />");
    Header.Controls.Add(new LiteralControl(sb.ToString()));

    Not sure this is how I would handle it, but it is a temporary solution. Wrap
    it in its own function so you can change the implementation if you find a
    better way.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    ***************************
    Think Outside the Box!
    ***************************


    "Arne" wrote:

    > I need literal controls in the <head> section of my page. I need to be able
    > to load dynamic values in the head section. It works fine in ASP.Net 1.1, but
    > Asp.net 2.0 gives a compile error.
    > I need the dynamically assigned <base href='??'> and
    > <META NAME="Description" CONTENT="??">
    > Is there a work around?
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN, Jan 10, 2006
    #3
  4. I have tried that, but header has a value of nothing in the page_load and
    prerender events.

    "Cowboy (Gregory A. Beamer) - MVP" wrote:

    > Just off the top of my head (which works), in Page_Load:
    >
    > StringBuilder sb = new StringBuilder();
    > string someName = "keywords";
    > string someKeywords = "blah, doh";
    > sb.Append("<meta name=\"");
    > sb.Append(someName);
    > sb.Append("\" content=\"");
    > sb.Append(someKeywords);
    > sb.Append("\" />");
    > Header.Controls.Add(new LiteralControl(sb.ToString()));
    >
    > Not sure this is how I would handle it, but it is a temporary solution. Wrap
    > it in its own function so you can change the implementation if you find a
    > better way.
    >
    > --
    > Gregory A. Beamer
    > MVP; MCP: +I, SE, SD, DBA
    >
    > ***************************
    > Think Outside the Box!
    > ***************************
    >
    >
    > "Arne" wrote:
    >
    > > I need literal controls in the <head> section of my page. I need to be able
    > > to load dynamic values in the head section. It works fine in ASP.Net 1.1, but
    > > Asp.net 2.0 gives a compile error.
    > > I need the dynamically assigned <base href='??'> and
    > > <META NAME="Description" CONTENT="??">
    > > Is there a work around?
    =?Utf-8?B?QXJuZQ==?=, Jan 10, 2006
    #4
  5. Hi Arne,

    the header has a value of null because inside the ASPX, there is no
    'runat=server' attribute present on the <head> tag.
    Try this:

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
    protected override void OnLoad(EventArgs e)
    {
    HtmlMeta meta = new HtmlMeta();
    meta.Name = "Description";
    meta.Content = "SomeRandomValue";
    Header.Controls.Add(meta);
    base.OnLoad(e);
    }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
    </body>
    </html>

    Grtz,

    Wouter van Vugt
    Trainer - Info Support
    http://blogs.infosupport.com/wouterv
    Wouter van Vugt, Jan 10, 2006
    #5
  6. You can also turn add the meta tags on the form and set runat="server", e.g.

    <head>
    <meta name="Description" runat="server" id="meta1"/>
    <base runat="server" id="base1"/>
    </head>

    then in the codebehind you would access them directly like this:

    meta1.Content = "My Site description";
    base1.Attributes.Add("href","http://www.website.com");
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Arne" wrote:

    > I need literal controls in the <head> section of my page. I need to be able
    > to load dynamic values in the head section. It works fine in ASP.Net 1.1, but
    > Asp.net 2.0 gives a compile error.
    > I need the dynamically assigned <base href='??'> and
    > <META NAME="Description" CONTENT="??">
    > Is there a work around?
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 10, 2006
    #6
    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. Brian W
    Replies:
    10
    Views:
    755
    Brian W
    Jul 2, 2003
  2. Sergey Morkovkin
    Replies:
    5
    Views:
    17,283
    gamcfarlane
    Jul 14, 2009
  3. showme

    PHP and ASP.NET go HEAD to HEAD

    showme, Jul 8, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    710
    Sherif ElMetainy
    Jul 10, 2004
  4. Karl
    Replies:
    0
    Views:
    385
  5. Jiho Han

    Write into <HEAD></HEAD> section?

    Jiho Han, Jan 14, 2004, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    221
    Sam Fields
    Jan 16, 2004
Loading...

Share This Page