global.asax和应用程序对象? [英] global.asax and application objects?

查看:81
本文介绍了global.asax和应用程序对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。


如果这是错误的群体,请道歉。我创建了一个名为

" page"的课程。我想添加到global.asax,然后从

调用网站内的任何控件。我现在有:


global.asax

<%@ import Namespace =" myNamespace" %>

< script language =" C#" runat =" server">

公共页面网页;


void Session_Start(Object sender,EventArgs E){

Response.Write(会话正在开始......< br>);

网页=新页面();

}

< / script>


我希望能够拨打网页。控件中的对象如

所以:


webpage.methodName();


然而我得到一个 ;找不到类型或命名空间错误。任何人都可以帮助我解决这个问题,或者建议在整个应用程序中建立一个更好的方法来持久保存

全局对象 - 我试图避免使用

" page webpage = new page();"在每个控件中。


谢谢,


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

推荐答案

嗨Marc,


首先,C#是区分大小写的,所以除非你定义了一个名为

" page"的类。你肯定会得到这个错误。
Hi Marc,

First, C# is case-sensitive, so unless you have defined a class called
"page" you would certainly get this error.
但是我得到了无法找到类型或命名空间错误。


其次,你还没有创建类的实例,只是包含类实例的

的变量。

第三,虽然Global.asax文件是在运行时编译并实例化为
作为类,但其目的不是用作业务类。业务

类应单独定义。 global.asax文件的目的是

来定义应用程序级事件的事件hsndlers,例如

Application_OnStart,Application_OnEnd,Session_OnStart和Session_OnEnd。

如果你想在应用程序范围中存储一个clss实例,你最好的b / b
赌注是在这些事件处理程序之一中创建实例并将其添加到

应用程序缓存。


第四,System.Web.UI.Page类专门设计为处理和响应HTTP请求的

HttpHandler。它不是设计为商业类的b $ b,不应该用作商业类。我无法想象为什么

任何人都想要,但我只能猜测你猜错了什么

错了(除了我有什么已经指出了。


如果你能告诉我们你想要实现什么样的功能,我们

可以提供最好的建议实现它的方法。与此同时,我建议下载免费的Microsoft .Net SDK,然后阅读

ASP.Net:

=nofollowhref =http://www.microsoft.com/downloads/details.aspx?FamilyId=9B3A2CA6-3647-4070-9F41-A333C6B9181D&displaylang=entarget =_ blank> http:// www .microsoft.com / downloads / d ... displaylang = en


-

HTH,


Kevin Spencer

Microsoft MVP

..Net开发人员

每个人都嗤之以鼻,

但是有些人擅长隐藏它。


" Marc" <毫安********** @ gmail.com>在消息中写道

news:11 ********************* @ f14g2000cwb.googlegro ups.com ...你好。

如果这是错误的群体,请道歉。我创建了一个名为
page的类。我想添加到global.asax,然后从
网站内的任何控件调用。我目前得到:

global.asax

<%@ import Namespace =" myNamespace" %>
< script language =" C#" runat =" server">
公共页面网页;

void Session_Start(Object sender,EventArgs E){
Response.Write(" Session is Starting ... < br>");
网页=新页面();
}
< / script>

我希望能够打电话给"网页"控件中的对象如
所以:

webpage.methodName();

然而我得到的是无法找到类型或命名空间错误。任何人都可以帮我解决这个问题,或者建议在整个应用程序中建立一个更好的持久化全局对象的方法 - 我试图避免使用
page pagepage = new page(); ;在每个控件中。

谢谢,

marc
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/d...displaylang=en

--
HTH,

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

"Marc" <ma**********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com... 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



你好。


感谢您的快速回复。我创建了一个Namespace.page类,其中包含与数据库交互并返回内容的方法

用于页面和菜单,图形等。以前在我的控件中我会有

说:


公共页面网页;

private void page_load(对象等等)

{

page =新网页(1);

lblHeading = page.getHeading();

lblBody = page.getBody();

dgMenu.DataSource = page.getMenuItems();

dgMenu.DataBind(); (等等等)

}


我想只创建这个类的一个实例并从

中调用它这听起来像是:


"如果你想在应用程序范围中存储一个clss实例,

最好

赌注是在其中一个事件处理程序中创建实例并添加它



应用程序缓存。


我想做什么。我试图这样做:


网页=新页面();

Cache.Insert("网页,网页,null,缓存。 NoAbsoluteExpiration,

TimeSpan.FromMinutes(15));

我的global.asax中的



现在我得到一个空对象引用错误 - 这回到你所说的
这里说:


"第二,你还没有创建一个类的实例,只是一个变量

for

包含该类的实例。


我不明白为什么它不是我班级的一个实例?我的案例

a小知识很危险吗?


感谢您的帮助!


ta


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




- 如果你想要一个已经准备好的对象在每个页面中使用(想要

来跳过执行initlization的代码),而不是让页面派生形式

a basepage,其中对象被实例化。
< a rel =nofollowhref =http://aspnet.4guysfromrolla.com/articles/041305-1.aspxtarget =_ blank> http://aspnet.4guysfromrolla.com/articles/041305-1.aspx

这个对象的生命周期是页面的生命周期所以也许它不是什么东西

你可以使用。


- 如果你想访问常见例程,而不是使用static / instance

方法创建类。

这也是存储全局范围数据的好选择因为你

在开发过程中获得intellisense。


如果你还有q请告诉我提示......


干杯,

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
你好。

如果这是错误的组,请道歉。我创建了一个名为
page的类。我想添加到global.asax,然后从
网站内的任何控件调用。我目前得到:

global.asax

<%@ import Namespace =" myNamespace" %>
< script language =" C#" runat =" server">
公共页面网页;
void Session_Start(Object sender,EventArgs E){
Response.Write(" Session is Starting ...< br> );
网页=新页面();
}
< / script>
我希望能够调用网页。控件中的对象如
所以:

webpage.methodName();

然而我得到的是无法找到类型或命名空间错误。任何人都可以帮我解决这个问题,或者建议在整个应用程序中建立一个更好的持久化全局对象的方法 - 我试图避免使用
page pagepage = new page(); ;在每个控件中。

谢谢,

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



这篇关于global.asax和应用程序对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆