HtmlTextWriter and Controls
Go back and review the control's Render method for a minute. Notice the Render method places literal font tags to change the color of the palindrome text. While this is certainly effective, this technique has a couple of downsides. For example, HTML is defined by multiple standards. That is, browsers running both HTML version 3.2 and 4.0 occur in nature. Certain HTML elements have changed between version 3.2 and 4.0. If you render all your HTML directly, expecting requests from a certain kind of browser, your users may be taken by surprise if they browse to your page with a new browser that interprets HTML differently.
NOTE
The .NET framework includes multiple versions of the HtmlTextWriter class: Html32TextWriter, HtmlTextWriter, XhtmlTextWriter, and ChtmlTextWriter. When a request comes from a browser, it always includes some header information indicating what kind of browser made the request. Most browsers these days are capable of interpreting the current version of HTML. In this case, ASP.NET passes in a normal HtmlTextWriter into the Render method. However, if you happen to get a request from a lesser browser that understands only HTML 3.2, ASP.NET passes in an Html32TextWriter. The classes are similar as far as their use and may be interchanged. Html32TextWriter emits certain tags (such as table tags) in HTML 3.2 format, while HtmlTextWriter emits the same tags in HTML4.0 format. Information within Machine.Config and the browser capabilities configuration help ASP.NET figure out what kind of HtmlTextWriter to use. The browser capability information deduced by the ASP.NET runtime may be used for more than simply selecting the correct HtmlTextWriter. The Request property (available as part of the HttpContext and the Page) includes a reference to the Browser object. This object includes a number of flags indicating various pieces of information such as the type of browser making the request, whether the browser supports scripting, and the name of the platform the browser is running on. This information comes down as part of the headers included with each request. The ASP.NET runtime runs the headers against some well-known regular expressions within the configuration files to figure out the capabilities. For example, here's a short listing illustrating how to figure out if the browser making the request supports Frames:
public class TestForFramesControl : Control { protected override void Render(HtmlTextWriter output) { if (Page.Request.Browser.Frames) { output.Write( "This browser supports Frames"); } else { output.Write("No Frames here"); } } }
To get a feel for using the more advanced capabilities of HtmlTextWriter, replace the hard-coded font tags in the Render method of the PalindromeCheckerRenderedControl with code that uses the HtmlTextWriter facilities.
Use the HtmlTextWriter
Open the PalindromeCheckerRenderedControl.cs file.
Update the Render method to use the HtmlTextWriter methods. Use HtmlTextWriter.RenderBeginTag to start a font tag and a bold tag. Use HtmlTextWriter.AddStyleAttribute to change the color of the font to blue.
protected override void Render(HtmlTextWriter output) { if (this.CheckForPalindrome()) { output.Write("This is a palindrome: <br>"); output.RenderBeginTag(HtmlTextWriterTag.Font); output.AddStyleAttribute(HtmlTextWriterStyle.Color, "blue"); output.RenderBeginTag(HtmlTextWriterTag.B); output.Write(Text); output.RenderEndTag(); // bold output.RenderEndTag(); // font } else { output.Write("This is a palindrome: <br>"); output.RenderBeginTag(HtmlTextWriterTag.Font); output.AddStyleAttribute(HtmlTextWriterStyle.Color, "blue"); output.RenderBeginTag(HtmlTextWriterTag.B); output.Write(Text); output.RenderEndTag(); // boldl output.RenderEndTag(); // font } }
The HtmlTextWriter class and the enumerations include support to hide all the oddities of switching between HTML 3.2 and 4.0. Listing 4-6 shows how the table is rendered using an HTML 4.0–compliant response. Listing 4-7 shows how the table is rendered using an HTML 3.2–compliant response.
Listing 4-6
Rendered Control: <br /> <br /> This is a palindrome: <br> <b><font>Do geese see god?</font></b><br> <table width="50%" border="1" style="color:blue;"> <tr> <td align="left" style="font-size:medium;color:blue;"> A man, a plan, a canal, panama.</td> </tr> <tr> <td align="left" style="font-size:medium;color:blue;"> Do geese see god?</td> </tr>
Listing 4-7
Rendered Control:<br /> <br /> This is a palindrome: <br> <b><font>Do geese see god?</font></b><br> <table width="50%" border="1""> <tr> <td align="left"> <font color="blue" size="4">A man, a plan, a canal, panama.</font> </td> </tr> <tr> <td align="left"><font color="blue" size="4">Do geese see god?</font> </td> </tr>