Controls in the <head> section

G

Guest

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?
 
K

Karl Seguin

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
 
G

Guest

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!
***************************
 
G

Guest

I have tried that, but header has a value of nothing in the page_load and
prerender events.
 
W

Wouter van Vugt

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
 
G

Guest

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");
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top