Friday, November 21, 2008
Response.WriteResponse.Write

Real quick: This is a reminder about a thing a lot of folks forget. I forget it all the time too so I'm putting it here in hopes that writing it will drill it into my head once and for all.

"Page_Load code gets called twice by default"

Copy this into a text file, rename the file with an ASPX extension, stick it in a VDir and run it.

<%@ Page Language="VB" %>
<script language="vb" runat="server">

   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles MyBase.Load
      
      PrintCookies()

   End Sub

   Private sub PrintCookies()

      Dim cookCol As New HttpCookieCollection()
      cookCol = Request.Cookies
      Dim s As String
      For Each s In cookCol
         response.write("Cookie: " & s & "<br><br>")
         response.write("Value:" & Request.Cookies(s).Value & "<br><br>")
         response.write("<br><br>")
      Next 
   
   End sub

</script>

<html>
<body>

<h3>Just a test of writing</h3>

</body>
</html>

Lookit that, you have two Session cookies :). Nope, it's the Page_Load firing twice and so your Response.Write (still the most used command in ASP/ASP.net development) happens twice.

To stop that annoyance, either remove the MyBase.Load Handler (adding more work down the line if you code in the IDE ... not a problem if you mostly use Notepad) and/or just explicitly switch off the AutoEventWireup page directive (it's on by default).

<%@ Page Language="VB" AutoEventWireup="false"%>

Robert Smith
Kirkland, WA

added to smithvoice april 2004


Print  

pagecomment
  Add Comment



Submit Comment
  View Ratings
50.00%0
40.00%0
30.00%0
20.00%0
10.00%0

Number of Comments 0 , Average of Ratings
  View Comments
No comment.


Privacy Statement  |  Terms Of Use
Copyright 2008 by Robert C. Smith