Page_Unload problem in .NET 1.1?

D

Dan Lichiardopol

Hi there!

I'm trying to use page unload event in order to write info to a text
file. Everything works fine, unless code contains a call to
Response.End. Then, the unload event appears to be triggered twice.

I have created a very simple test page that illustrates this behaviour.
The problem does not occur under .NET 2.0. If you would examine the
logfile.txt created by the page running under 1.1, you will notice two
entries for unload event.

I'm not sure that is this really a bug or something that was supposed
to be this way. Is this, by any chance, a documented issue? I could not
find anything related to it. The easy way out, would be to upgrade to
..NET 2.0 - but that is not an option at the time being. Any suggestions
on a workaround, would be appreciated.

Thanks,
Dan

Test page code follows:

--- code begin ---
<%@ Page Language="vb"%>
<%@ Import Namespace="System.IO" %>
<%
Response.Write("Running .NET ver. " &
System.Environment.Version.ToString() & "<br />")
Response.Write("Before calling Response.End()<br />")
Response.End() ' triggers OnUnload twice on .NET 1.1
Response.Write("After calling Response.End()<br />") ' should never
show
%>
<script language="vb" runat="server">
protected sub Page_Unload(sender as object, e as EventArgs)
dim s as StreamWriter
s = File.AppendText(Server.MapPath("logfile.txt"))
s.Writeline("OnUnload")
s.Close()
end sub
</script>
--- code end ---

Testing environment:
-----------------------------
I have .net 1.1 and 2.0 installed on an IIS 5.1 running under XP
Professional. Two application domains were created one for 1.1 and
another one for 2.0. No modifications were done to web.config file.
(aspnet user must have rights to create & modify files under the app
domain).
 
M

Marina Levit [MVP]

I have no idea if this is a bug or not. If it is indeed a bug, I would
suggest having a page level variable that is a boolean that keeps track of
whether or not Page_Unload has run. Set it in your handler, and in the
beginning always check to make sure it hasn't been run already. If it has,
just do nothing. This would at least avoid having your code run twice.
 
D

danl

Thanks Marina,

That's definitely an option.
I have no idea if this is a bug or not. If it is indeed a bug, I would
suggest having a page level variable that is a boolean that keeps track of
whether or not Page_Unload has run. Set it in your handler, and in the
beginning always check to make sure it hasn't been run already. If it has,
just do nothing. This would at least avoid having your code run twice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top