output.aspx
<%@ Page Language="VB" %>
<script language="VB" runat="server">
' OK, here's the scenario:
' We're happily scripting along and all of a sudden we need to
' output the value of the variable strSomeText inside a bold tag.
' This naturally means we need to output some plain HTML for the
' beginning and end <strong> tags.
' Create the variable that will contain the text that we want to
' display and put some text into it:
Dim strSomeText As String = "This is some text"
Sub Page_Load(sender as Object, e as EventArgs)
' Methods 1 and 2 don't do anything here... take a look at
' the in-line code below.
' Method 3:
' Set the text of our label control in method 3.
lblMethod3.Text = strSomeText & " in a Label control that was formatted at design-time."
' Set the text of our second label control in
' method 3 and then make it bold.
lblMethod3Format.Text = strSomeText & " in a Label control that was formatted at run-time."
lblMethod3Format.Font.Bold = True
' Set the text of our literal control in method 3.
litMethod3.Text = strSomeText & " in a Literal control."
End Sub
</script>
<html>
<head>
<title>ASP.NET Output Sample</title>
</head>
<body>
<form runat="server">
<p>
Method 1 - Break Script:<br />
<%
' *** Method 1 - Break Script ***
' We stop our scripting, drop out to the HTML that will be sent
' directly to the browser and then just type our HTML tags as we
' normally would in a plain HTML page. This is actually done
' twice in this instance. Once for the <strong> and once for
' the </strong>.
%><strong><%
Response.Write(strSomeText)
%></strong><%
' Note: You'll usually see it formatted more like this:
'
' % >
' <strong>< % Response.Write(strSomeText) % ></strong>
' < %
'
' the two are functionally equivalent, the only difference is the
' placement of the carriage returns. The first is shown to more
' clearly illustrate where we stop and resume the script each time.
' *** End Method 1 ***
%>
</p>
<p>
Method 2 - Response.Write:<br />
<%
' *** Method 2 - Response.Write ***
' In this method we never drop out of our script. We accomplish
' this by simply outputting the HTML as strings along with the
' value of our variable.
Response.Write("<strong>")
Response.Write(strSomeText)
Response.Write("</strong>")
' Note: once again, the formatting is normally a little different:
'
' Response.Write("<strong>" & strSomeText & "</strong>")
'
' Once again, the two are pretty much the same. As before, I'm only
' using the first format in an attempt to more clearly illustrate
' what is actually happening.
' *** End Method 2 ***
%>
</p>
<p>
Method 3 - The ASP.NET Way:<br />
<%
' *** Method 3 - The ASP.NET Way ***
' In this method we use an ASP.NET server control to handle our
' appointed task. The exact control you use and how you format
' it are really up to you and can depend on your situation, but
' I'll illustrate a couple different methods so you can choose
' what will work best for you. Here is a Label control with the
' bold applied when the control is defined.
%>
<asp:Label id="lblMethod3" Font-Bold="True" runat="server" />
<br />
<%
' This time we'll once again use a Label control, but will
' make the text bold at runtime instead. See the Page_Load
' subroutine above.
%>
<asp:Label id="lblMethod3Format" runat="server" />
<br />
<%
' While the label control works very well, when I need to be in
' complete control of the HTML that is sent the the browser, I
' very often use the literal control because it outputs exactly
' what you tell it to and nothing else. The down side is that
' you can't apply formatting to it like you can with the Label
' control so which one you use really depends on your situation.
%>
<strong>
<asp:Literal id="litMethod3" runat="server" />
</strong>
<%
' *** End Method 3 ***
%>
</p>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/output_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>