Finding the head tag of a page

J

Jeremy

I've written a method that recursively loops through the control collections
on a page looking for a literal control called html.
Sometimes the html literal control will contain a child literal control
called head. Sometimes the html literal control will not have a child
literal control called head, and instead will have the head html included in
its text property. Why would asp.net sometimes parse the pages html into
literal controls and sometimes not?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Use doctype to force browser into standard mode. Standard mode handles
css inheritance correctly. -->
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>test</title></head>
<body>
<form id="Form1" runat="server"></form>
</body>
</html>
 
B

bruce barker

in general, all the html between server controls is loaded into one html
literal. any html tag with a runat=server becomes its own html literal.

now some control parse their inner html and produce seperate control. for
example, the <table runat=server> control will convert all <tr> and <td> into
controls, and ignores everything else. the <head runat=server> will
automatically discover the title.

in your case some pages have the runat=server on the head and some don't.

note: the standard vs2005 page template create a <head runat=server>




-- bruce (sqlwork.com)
 
D

David R. Longnecker

The Page object for each .NET web form automatically has a Header (which
is an HtmlHeader object) associated to it if your <head /> tag has runat="server"
in it. From there, you can find things such as the Title and StyleSheets.

If you wanted to inject controls, such as a literal control, into the Header,
you can by adding the control to the Header's ControlCollection.

LiteralControl testControl = new LiteralControl("<junk>Test</junk>");
Page.Header.Controls.Add(testControl);

generates:

<head>
<title>Untitled Page</title>
<junk>Test</junk>
</head>

If this isn't what you're looking for, could you provide a bit of background
on what you're trying to do?

HTH.

-dl
 

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

Top