Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
[prev. Lesson]  Separate Code and HTML
     [next Lesson]  Visual Inheritance: Separate Code/Presentation

Why Literals Blow Away Response.write!
by Charles Carroll

When an ASP Classic programmer comes to ASP.net they tend to use Response.write out of reflex. In some situations it works and in many it does not for example:

  • <%=%> and response.writes just output nothing if embedded in a server control (the table:cell in the response.write example).

  • It IS slower than literals. I have done extensive tests I will put on the site soon.

  • It allows mixing of code and presentation/HTML which leads to may architectural problems.

But the thing that makes Literals better are:

  • The graphic designer can move around the literal as just one tag. Whereas with response.write they would have to somehow understand what represented "all" the related code and then mark many lines of code to move them (Error prone because they may forget a piece).

  • Literals can be carried with ViewState. Response.writes cannot.

  • Literals can mean that your code can add them to them many times in many code locations before the page is rendered. Response.write cannot.

Here is an example with literals: 

   filename=/experiments/literals/correctamundo.aspx

<Test Script Below>


<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
    DIM counter as integer
    DIM sb1 as new stringbuilder
    FOR counter=1 TO 100
        sb1.Append(counter)
        IF counter MOD 20 = 0 THEN
            sb1.Append("<br>")
        ELSE
            sb1.Append("&nbsp;")
        END IF
    NEXT COUNTER
    lit1.Text=sb1.ToString()
    litTime.text=DateTime.Now.ToString("G")
End Sub
</script>
<html><head>
<title>Literals Rock</title>
</head>
<body bgcolor="#FFFFFF">
<form runat="server">
<asp:literal id="lit1" runat="server"/>
<asp:Table runat="server" GridLines="both" BorderWidth="1px">
<asp:TableRow>
<asp:TableCell>Hello</asp:TableCell>
<asp:TableCell>Konnichi Wa</asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>Goodnight</asp:TableCell>
<asp:TableCell>Oyasumi</asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>The Time is:</asp:TableCell>
<asp:TableCell><asp:literal id="litTime" runat="server"/></asp:TableCell>
</asp:TableRow>
</asp:Table>

</form>
</body></html>
Here is a worse version (Response Write) example.
   filename=/experiments/literals/justplainwrong.aspx

<Test Script Below>


<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
End Sub
</script>
<html><head>
<title>Literals Rock</title>
</head>
<body bgcolor="#FFFFFF">
<form runat="server">
<%
    DIM counter as integer
    FOR counter=1 TO 100
        response.write(counter)
        IF counter MOD 20 = 0 THEN
            response.write("<br>")
        ELSE
            response.write("&nbsp;")
        END IF
    NEXT COUNTER
%>
<asp:Table runat="server" GridLines="both" BorderWidth="1px">
<asp:TableRow>
<asp:TableCell>Hello</asp:TableCell>
<asp:TableCell>Konnichi Wa</asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>Goodnight</asp:TableCell>
<asp:TableCell>Oyasumi</asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>The Time is:</asp:TableCell>
<asp:TableCell><%=now()%></asp:TableCell>
</asp:TableRow>
</asp:Table>
</form>
</body></html>

There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.