Rendering Output
As mentioned earlier, you have full responsibility for ensuring that the contents of a rendered custom control are drawn appropriately. This gives you more power than you have with user controls and composite custom controls, but also requires more work. All of the drawing occurs in the Render method, which enables you to emit HTML directly. Listing 2 shows how to implement the visual appearance of the RatingControl:
Listing 2 Drawing HTML for RatingControl
/// <summary> /// Render this control to the output parameter specified. /// </summary> /// <param name="output"> /// Response stream /// </param> protected override void Render(HtmlTextWriter output) { string choices = @" <input type=""radio"" name=""rating"" Value=""1""/> 1 <input type=""radio"" name=""rating"" Value=""2""/> 2 <input type=""radio"" name=""rating"" Value=""3""/> 3 <input type=""radio"" name=""rating"" Value=""4""/> 4 <input type=""radio"" name=""rating"" Value=""5""/> 5 "; string checkedChoice = "Value=\"" + Rating + "\""; choices = choices.Replace( checkedChoice, checkedChoice + " CHECKED"); output.Write(Text + "<br>"); output.Write(choices); output.WriteBeginTag("input"); output.WriteAttribute("name", this.UniqueID); output.WriteAttribute("type", "button"); output.WriteAttribute("value", "Cast Your Vote"); output.WriteAttribute("onclick", "javascript:" + Page.GetPostBackEventReference( this, "VoteButton")); output.WriteEndTag("input"); }
Listing 2 demonstrates a few ways to draw output in the Render method. The first two calls to output.Write demonstrate how to draw raw text with the HtmlTextWriter parameter. To make the drawing easier and potentially less error-prone, the HtmlTextWriter offers convenience methods for specifying tags and attributes. Listing 2 uses these convenience methods to draw the submit button. The HtmlTextWriter also includes RenderBeginTag, RenderEndTag, and other methods that allow more flexibility in the way the control is drawn, as well as support for multiple browsers.
TIP
The output from the Render method is drawn directly to the caller's browser and not streamed through ASP.NET. Thus, the markup for ASP.NET controls is not processed and won't appear correctly in the user's browser. You can observe this problem by drawing an ASP.NET control (such as a label) with the HtmlTextWriter and observing the output in the browser when a page with your control is requested. The ASP.NET control won't appear on the page, but by viewing the source you can see the same HTML that was rendered in your control. Normal HTML is the only output that will appear properly.
The Replace operation on the choices string ensures that the current choice will be selected when the control is drawn, based on the value of the Rating property. I'll discuss that property in the next section.
The WriteAttribute method call with the attribute parameter onclick enables the code to generate client-side script so the page can do a post back when the button is clicked. I'll discuss this attribute further in the section "Adding Event Infrastructure."