Overriding Render The MetaTag Control Example


Overriding Render ”The MetaTag Control Example

As an example of a control that generates a nonvisual HTML element, we'll derive from Control to create a MetaTag control that renders as a <meta> tag in the <head> section of an HTML page. Listing 8-1 contains the code for the MetaTag control.

Listing 8-1 MetaTag.cs
 usingSystem; usingSystem.ComponentModel; usingSystem.Web.UI; namespaceMSPress.ServerControls{ [ DefaultProperty("Name") ] publicclassMetaTag:Control{ [ Category("Behavior"), DefaultValue(""), Description("Thenameofthemetatag") ] publicstringName{ get{ strings=(string)ViewState["Name"]; return(s==null)?String.Empty:s; } set{ ViewState["Name"]=value; } 
 } [ Category("Default"), DefaultValue(""), Description("Thedataorvalueassociatedwiththemetatag") ] publicstringContent{ get{ strings=(string)ViewState["Content"]; return(s==null)?String.Empty:s; } set{ ViewState["Content"]=value; } } //Thisensuresthattherearenochildcontrols. protectedoverrideControlCollectionCreateControlCollection(){ returnnewEmptyControlCollection(this); } protectedoverridevoidRender(HtmlTextWriterwriter){ stringname=Name; if(name.Length==0){ thrownewInvalidProgramException("TheNameproperty" +  " oftheMetaTagcontrolmustbeset."); } writer.AddAttribute(HtmlTextWriterAttribute.Name,name); writer.AddAttribute("content",Content); writer.RenderBeginTag(HtmlTextWriterTag.Meta); writer.RenderEndTag(); } } } 

When you derive from the Control class, you override the Render method to write markup text to the response stream. In Chapter 5, when overriding the Render method in the PrimeGenerator example (shown in Listing 5-3), we directly wrote HTML markup using the Write method of the HtmlTextWriter object. In the MetaTag example, we do not directly write HTML but instead use the tag-rendering methods of the HtmlTextWriter object. We also use the Html ­TextWriterAttribute and HtmlTextWriterTag enumerations in conjunction with the tag-rendering functionality. The HtmlTextWriter class and its related enumerations simplify formatting, help to reduce errors, and make your code more readable.

Note that we didn't invoke the Render method of the base class or the RenderChildren method because the MetaTag control does not have any child controls in its Controls collection. (We overrode the CreateControlCollection method to return an instance of the EmptyControlCollection class, which ensures that the Controls collection is empty by disallowing a user from adding child controls to our control.)

Multiple Calls to HtmlTextWriter vs. String Concatenation

When you write text into an HtmlTextWriter object, it is more efficient to make multiple calls to the Write method of that object than to concatenate strings before writing them. String concatenation consumes time and memory (because strings are immutable) and is unnecessary when writing to a stream. This is an important efficiency consideration that new control authors often overlook. The PrimeGenerator control in Chapter 5 (shown in Listing 5-3) demonstrates successive calls that write to the output stream.

At times, it is necessary to combine strings ”for example, when you want to build up an attribute value. In those cases, use string concatenation if the number of operations is small; otherwise , use the System.Text.StringBuilder class, which provides an optimized implementation of string concatenation. However, you should not use StringBuilder to build a string to pass into the Write method, because the underlying response stream already provides that functionality when you make multiple calls to Write .

Listing 8-2 shows a page that uses the MetaTag control.

Listing 8-2 MetaTagTest.aspx
 <%@PageLanguage="C#" %> <%@RegisterTagPrefix="msp"  Namespace="MSPress.ServerControls" Assembly="MSPress.ServerControls" %> <html> <head> <msp:MetaTagname="author"  content="NikhilKothariandVandanaDatye"  runat="server" id="metaTag1" /> </head> <body> <br> 
 ThispageusestheMetaTagcontrolinitsheadsection. SelectViewSourceinyourWebbrowsertoviewthecontrol renderedasa&lt;meta&gt;tag. <br> </body> </html> 

If you access MetaTagTest.aspx in your browser and view the HTML source for the rendered page, you will see the <meta> tag rendered by the MetaTag control in the <head> section of the rendered HTML content:

 <head> <metaname="author" content="NikhilKothariandVandanaDatye" /> </head> 


Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net