global.asax and application objects?

M

Marc

Hello.

Apologies if this is the wrong group. I have created a class called
"page" which i would like to add to the global.asax and then call from
any control within the website. I have currently got:

global.asax

<%@ import Namespace="myNamespace" %>
<script language="C#" runat="server">
public page webpage;

void Session_Start(Object sender, EventArgs E) {
Response.Write("Session is Starting...<br>");
webpage = new page();
}
</script>

I then want to be able to call the "webpage" object in a control like
so:

webpage.methodName();

However i get a "type or namespace cannot be found" error. Can anyone
help me fix this or perhaps suggest a better method of persisting a
global object throughout the application - i am trying to avoid having
"page webpage = new page();" in each control.

Thanks,

marc
 
K

Kevin Spencer

Hi Marc,

First, C# is case-sensitive, so unless you have defined a class called
"page" you would certainly get this error.
However i get a "type or namespace cannot be found" error.

Second, you have not created an instance of the class, just a variable for
containing an instance of the class.

Third, while the Global.asax file is compiled at run-time and instantiated
as a class, its purpose is not to be used as a business class. Business
classes should be defined separately. The purpose of the global.asax file is
to define event hsndlers for Application-level events, such as
Application_OnStart, Application_OnEnd, Session_OnStart and Session_OnEnd.
If you want to store an instance of a clsss in Application Scope, your best
bet is to create the instance in one of these event handlers and add it to
the Application Cache.

Fourth, a System.Web.UI.Page class is specifically designed as an
HttpHandler that handles and responds to HTTP Requests. It is not designed
as a business class, and should not be used as one. I can't imagine why
anyone would want to, but I can only guess that you're guessing something
wrong (in addition to what I've already pointed out).

If you can tell us what sort of functionality you're trying to achieve, we
can provide suggestions as to the best way to achieve it. In the meantime, I
would suggest downloading the free Microsoft .Net SDK, and reading up on
ASP.Net:

http://www.microsoft.com/downloads/...A6-3647-4070-9F41-A333C6B9181D&displaylang=en

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.
 
M

Marc

Hello.

Thanks for the quick reply. I have created a Namespace.page class which
includes methods for interacting with a database and returning content
for pages and menu, graphic etc. Previously in my controls i would have
said:

public page webpage;
private void page_load(object etc etc etc)
{
page = new webpage(1);
lblHeading = page.getHeading();
lblBody = page.getBody();
dgMenu.DataSource = page.getMenuItems();
dgMenu.DataBind(); (etc etc etc)
}

I want to only create a single instance of this class and call it from
any control so it sounds like:

" If you want to store an instance of a clsss in Application Scope,
your best
bet is to create the instance in one of these event handlers and add it
to
the Application Cache. "

Is what i want to do. I have attempted to do this by:

webpage = new page();
Cache.Insert ("webpage", webpage, null, Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes (15));

in my global.asax.

Now i get a null object reference error - which comes back to what you
said here:

"Second, you have not created an instance of the class, just a variable
for
containing an instance of the class. "

Just i dont understand why it is not an instance of my class? I case of
a little knowledge is dangerous?

Thanks for any help!

ta

marc
 
T

tom pester

- If you want to have an object that's ready for use in each page (tou want
to skip the code that does the initlization) than let the page derive form
a basepage where the object is instantiated.
http://aspnet.4guysfromrolla.com/articles/041305-1.aspx
This object will have a lifetime of that of the page so maybe its not something
you can use.

- If you want to have access to common routines than make a class with static/instance
methods.
This is also a good option for storing data with global scope because you
get intellisense while developing.

Let me know if you have any more questions...

Cheers,
Tom Pester
 
K

Kevin Spencer

Take a look at my first comment. System.Web.UI.Page starts with a capital
"P". C# is case-sensitive.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.
 
M

Marc

"Take a look at my first comment. System.Web.UI.Page starts with a
capital
"P". C# is case-sensitive. "

Hello.

I am not sure where this applies as i have created a class called
myNameSpace.page, which is the one i am trying to work with.

Anyway - i think i have managed to inherit from a baseclass. I have a
baseclass of:

public class page : System.Web.UI.Page
{
protected override void OnLoad (EventArgs e)
{
System.Web.HttpContext.Current.Response.Write("message from
baseclass");
}

public string getString()
{
string str = "this is a string";
return str;
}
}

Which i have inherited into my page using <%@ Page Language="C#"
Debug="true" Inherits="NameSpace.page"%>.

Now each page outputs "message from baseclass". But, when i try to use
the getString method i get an error:

"CS1519: Invalid token '(' in class, struct, or interface member
declaration"

Any ideas anyone?

ta
 
K

Kevin Spencer

Hi Marc,
"CS1519: Invalid token '(' in class, struct, or interface member
declaration"

Your base class looks fine. Can we see the code for your derived class?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Paranoia is just a state of mind.
 
K

Kevin Spencer

Now, how can you be an idiot if you figured it out for yourself?

--
;-),

Kevin Spencer
Microsoft MVP
..Net Developer
Paranoia is just a state of mind.
 
J

Juan T. Llibre

Kevin,

everybody is free to consider themselves as
whatever they wish to consider themselves as... ;-)

But, I do agree with you... !
 

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

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top