Programming Microsoft ASP.NET 4 - Dino Esposito [129]
Finally, HtmlHead features a StyleSheet property of type IStyleSheet. The actual class that implements the interface is internal and named StyleSheetInternal. All this class does is let you create CSS style information programmatically. Note that the StyleSheet property is not a programmatic way to link a URL to an external CSS file. It is, instead, an API for you to create an ASP.NET–specific Style object that is then translated into a CSS block within the page. Here’s an example:
protected void Page_Load(object sender, EventArgs e)
{
var myAreaStyle = new Style {ForeColor = Color.Blue, BackColor = Color.LightGray};
// Add the style to the header of the current page
Page.Header.StyleSheet.CreateStyleRule(myAreaStyle, null, "DIV#MyArea");
}
The resulting page header looks like this:
The RegisterStyle method allows registered page-wide Style objects to be merged programmatically with the style object of individual server controls. You register a new control-focused style as shown here:
var labelStyle = new Style { ... };
...
Page.Header.StyleSheet.RegisterStyle(labelStyle, null);
// Right after registering the style, you apply it to or merge it with multiple controls
Label1.ApplyStyle(labelStyle);
Label2.MergeStyle(labelStyle);
Suppose now your page includes the following controls:
Here’s the markup generated for the two Label controls:
Hello, world
Hello, world
The class attribute of the first control is set to an autogenerated CSS class; the class attribute of the second control is the result of merging the current style with the new one.
Linking External CSS Files
To link an external style sheet file, you use the following code:
var link = new HtmlLink {Href = "~/StyleSheet.css"};
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(link);
The HtmlLink control represents an individual element. The tag can appear only in the
section of a document, although it can appear any number of times.Managing Meta Information
The HtmlMeta control is a helper object to allow programmatic control over the HTML element. Located within the
// Meta information providing some clue to search engines
var meta1 = new HtmlMeta
{
Name = "keywords",
Content = "Key terms that describe your page"
};
Page.Header.Controls.Add(meta1);
You can use the Scheme property to define some content for the scheme attribute of the HTML tag to provide additional information to user agents on how to interpret the meta information.
Finally, you use the HttpEquiv property instead of Name when you need to assign a value to the http-equiv attribute of the resulting HTML element.
Important
If your
section contains code blocks, you are not allowed to enter changes to its structure, such as adding new controls for and tags. If you do so, you’ll get a run-time exception. A common reason to have code blocks in a server-side tag is to resolve script URLs. Here’s a common example:This code just prevents you from programmatically adding new controls to the Header object. The workaround simply consists of moving the script tag away from the
block. A