Can't get Global.asax to execute Sub

T

tshad

I have an example I copied from "programming asp.net" (o'reilly) and can't
seem to get the Sub (writefile) to execute. It displays all the
response.write lines that are called directly, but not any of the
response.write lines from inside the sub.

*******************************************
<%@ Application Language="VB" %>

<script runat="server">

protected sub Application_Start(ByVal Sender as Object, _
ByVal e as EventArgs)
WriteFile("Application Starting")
end sub

protected sub Application_End(ByVal Sender as Object, _
ByVal e as EventArgs)
WriteFile("Application Ending")
end sub

protected sub Session_Start(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Session_Start" + "<br/>")
end sub

protected sub Session_End(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Session_End" + "<br/>")
end sub

protected sub Application_Disposed(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Application_Disposed" + "<br/>")
end sub

protected sub Application_Error(ByVal Sender as Object, _
ByVal e as EventArgs)
dim strError as string
strError = Server.GetLastError().ToString()

Context.ClearError()

Response.Write("Application_Error" + "<br/>")
Response.Write("Error Msg: " & strError + "<br/>")
end sub

protected sub Application_BeginRequest(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Application_BeginRequest" + "<br/>")
end sub

protected sub Application_EndRequest(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Application_EndRequest" + "<br/>")
end sub

protected sub Application_AcquireRequestState(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Application_AcquireRequestState" + "<br/>")
end sub

protected sub Application_AuthenticateRequest(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Application_AuthenticateRequest" + "<br/>")
end sub

protected sub Application_AuthorizeRequest(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Application_AuthorizeRequest" + "<br/>")
end sub

protected sub Application_PostRequestHandlerExecute(ByVal Sender as
Object, _
ByVal e as EventArgs)
Response.Write("Application_PostRequestHandlerExecute" + "<br/>")
end sub

protected sub Application_PreRequestHandlerExecute(ByVal Sender as
Object, _
ByVal e as EventArgs)
Response.Write("Application_PreRequestHandlerExecute" + "<br/>")
end sub

protected sub Application_PreSendRequestContent(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Application_PreSendRequestContent" + "<br/>")
end sub

protected sub Application_PreSendRequestHeaders(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Application_PreSendRequestHeaders" + "<br/>")
end sub

protected sub Application_ReleaseRequestState(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Application_ReleaseRequestState" + "<br/>")
end sub

protected sub Application_ResolveRequestCache(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Application_ResolveRequestCache" + "<br/>")
end sub

protected sub Application_UpdateRequestCache(ByVal Sender as Object, _
ByVal e as EventArgs)
Response.Write("Application_UpdateRequestCache" + "<br/>")
end sub

sub WriteFile(strText as string)
response.write("inside WriteFile <br>")
end sub

</script>
******************************************************************

Do I need to do something else to make this work?

It is a straight global.asax file in the root folder that is executing
correctly on my aspx page (except for the sub). At the moment I am passing
a string, but since I couldn't get it to work I just put a response.write
line in the sub.

Thanks,

Tom
 
K

Karl Seguin

TShad:
You can't Response.Write within Application_Start...this happens outside the
page handling (more or less). It might be helpful if you explained why you
aren't doing this in Page_Load which is where this sorta stuff belongs....

Karl
 
T

tshad

Karl Seguin said:
TShad:
You can't Response.Write within Application_Start...this happens outside
the
page handling (more or less). It might be helpful if you explained why
you
aren't doing this in Page_Load which is where this sorta stuff belongs....

I am just trying to play with the Global.asax to understand how to set it up
and make some global routines. This was taken from O'Reillys book, but I
took out the write to a text file as that didn't seem to work (I assume as
it was originally writing to the C root.

I was just trying to get the sub to do something. But you explained why it
didn't work where it was. When I call it from the other events, it worked
fine.

Thanks,

Tom
 
K

Karl Seguin

Global routines should probably be placed inside their own utility class,
such as:

public sealed class Utility{
private void Utility(){}

public static void WriteLine(string value){
HttpContext.Current.Response.Write(value);
}
...
}


which can then be called via:

Utility.WriteLine("yyy");


Karl
 
T

tshad

Karl Seguin said:
Global routines should probably be placed inside their own utility class,
such as:

I agree.

I just haven't gotten around to building a Class for this yet. I wanted to
use Global.asax so as not to have to define a class and set up a code-behind
page for it. Not sure how to deal with that yet.

Thanks,

Tom
 
K

Kevin Spencer

I just haven't gotten around to building a Class for this yet. I wanted
to use Global.asax so as not to have to define a class and set up a
code-behind page for it. Not sure how to deal with that yet.

That's like saying that your building a house, and you put the toilet in the
garage so that you wouldn't have to add an extra room. Instead, think of the
needs of the house.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
T

tshad

Kevin Spencer said:
That's like saying that your building a house, and you put the toilet in
the garage so that you wouldn't have to add an extra room. Instead, think
of the needs of the house.

Actually, I am looking at the needs of the house, just not all at once.

I want to understand what is best to put in the Global.asax and what is best
in a class. So I am trying to get the Global to work first and understand
that and then build a class (actually I am building a database class now)
and play with both.

Thanks,

Tom
 
I

IPGrunt

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:[email protected]...

I agree.

I just haven't gotten around to building a Class for this yet. I wanted to
use Global.asax so as not to have to define a class and set up a code-behind
page for it. Not sure how to deal with that yet.

Thanks,

Tom

Tom,

Are you still fighting this battle?

With a static class like the one Karl describes, there is no "code-
behind" per se...the Util.cs file is the ONLY file. In other words,
there's no ASPX file associated with a static class like this.

-- ipgrunt
 
T

tshad

Tom,
Are you still fighting this battle?

With a static class like the one Karl describes, there is no "code-
behind" per se...the Util.cs file is the ONLY file. In other words,
there's no ASPX file associated with a static class like this.
Yup.

I am trying to understand both (code-behind and classes by themselves).

I just got VS trial version and am building a myUtils.vb file that has a
database class in it. I also have a Global.asax (not compiled yet) to
understand shared functions. I wasn't sure how to set it up in VS so I
decided to go this way first. Since I assume that Global.asax and
Global.asax.vb is essentially the same, except that in Global.asax.vb I
don't need the script tags.

In my other post, I have the function being called (which is farther that I
got before) but I am getting an error.

I decided to start with the database class as I have about 50 pages built
(no code-behind yet) but lots of database calls and I would like to simplify
these.

I have to start somewhere.

Tom
 
K

Kevin Spencer

I want to understand what is best to put in the Global.asax and what is
best in a class. So I am trying to get the Global to work first and
understand that and then

The Global.asax file IS a class. It is a collection of Application-level
event handlers. It is specific to the application.

If you think of classes the way you think of files and folders in your file
system, that may help. Just as folders and sub-folders in your computer make
finding a specific file easier, so classes should be organized in such a way
that you can easily identify the class that handles a given realm of
functionality. A class is a group of related data and process. So, if the
Global class is a collection of Application-level event handlers, that is
what it should be. A Page class is a collection of related UI data and
process that relates to an individual page Request and Response.

If you look at the CLR, you can see excellent examples of class
organization. The Math class, for example, is not much more than a bunch of
static Math functions. The File class has all the functionality needed to
work with Files. So, it wouldn't make sense to look in the Math class for a
function to open a file than it would to put your toilet in the garage.

I hope that clears things up a bit.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top