Finding the head tag of a page



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 sometimes parse the pages html into
literal controls and sometimes not?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<!-- 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="">
<form id="Form1" runat="server"></form>



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 (



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


<title>Untitled Page</title>

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



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