When you start creating data-driven ASP.NET sites, you'll often need to dynamically generate HTML content for your pages. Perhaps you need to store item description information, as HTML, in a database table. At runtime, you need to be able to display that content, rendered correctly as HTML. The Literal control can render HTML at runtime, allowing you to inject HTML into a page.
The sample page, LiteralControl.aspx, allows you to enter HTML into a TextBox control. When you click Display in Literal Control, you execute this event procedure, copying the text into a Literal control:
Private Sub btnAssign_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnAssign.Click litHTML.Text = txtHTML.Text End Sub
That's all it takes to insert HTML into a page at runtime. (Imagine that the HTML was coming from a table, rather than from a TextBox control. Things get awfully powerful, awfully quickly. You can render an entire page from a database, modifying the layout by changing the data in a table. The possibilities are huge.) Figure 22.4 demonstrates the technique, rendering a bulleted list from HTML entered into the text box on the page.
Figure 22.4. Use the Literal control to inject HTML into a page. The bulleted list on this page gets its HTML from the TextBox control at the top.