Overriding Render ”The MetaTag Control ExampleAs 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.csusingSystem; 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.)
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<meta>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> |