Subscribe

RSS Feed (xml)

Dynamically Adding Literal Text or HTML to a Web Form in ASP.NET

Use the System.Web.UI.WebControls.Literal control to place literal text anywhere
on a Web Form.The ASPX page is as follows:

<html>
<head>
<title><asp:Literal id=”litTitle” runat=”server” /></title>
</head>
<body></body>
</html>
In <script runat=”server” /> block or codebehind:
Sub Page_Load(sender As Object, e As EventArgs)
litTitle.Text = “This is my page title.”
End Sub

The easiest way to place some content on a Web page when you want to have absolute
control over the formatting (for example, you don’t want your text wrapped in a <div>
tag or in other HTML tags) is to use the Literal control in the System.Web.UI.
WebControls namespace. Another option that you can use is data-binding, which you
use by adding <%# VariableName %> to the page and ensuring that Me.DataBind() is
called somewhere in the page’s execution (typically during Page_Load). Usually the
Literal is a better solution, however, because data-binding the page will force all the
controls on the page to data-bind as well, and this can cause unexpected results or inefficient
use of your controls.You can also use the LabelWeb Server Control to display
dynamic text.

No comments:

Variety in the Web World