首页 新闻 会员 周边

如何引用Global.asax文件中的Object标记定义的对象

0
悬赏园豆:5 [已关闭问题]

个人觉得object标记应该就是定义一个全局静态变量.那定义后,我应该如何引用它?

在Application_Start事件中,我尝试对它初始化,VS又提示是只读属性.

那我应该如何对它进行初始化?

 

<%@ Application Language="C#" %>
<object Id="gObject1" Class="System.Int32" RunAt="server" Scope="application"/>
<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        //在应用程序启动时运行的代码
        this.gObject1= 0;  //VS的IntelliSense又提示属性是只读的..
        
    }
   
    void Application_End(object sender, EventArgs e)
    {
        //在应用程序关闭时运行的代码

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        //在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e)
    {
        //在新会话启动时运行的代码

    }

    void Session_End(object sender, EventArgs e)
    {
        //在会话结束时运行的代码。
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式
        //设置为 StateServer 或 SQLServer,则不会引发该事件。

    }
      
</script>

野蛮的主页 野蛮 | 初学一级 | 园豆:165
提问于:2009-09-07 14:46
< >
分享
其他回答(1)
0

你不如直接在你的Global的cs文件中定义一个public static的int变量.

I,Robot | 园豆:9783 (大侠五级) | 2009-09-07 14:56
我是想了解一下这个Object其中的道理..
支持(0) 反对(0) 野蛮 | 园豆:165 (初学一级) | 2009-09-08 11:33
0

静态方法不对特定的实例操作,只能访问静态成员,所以不能用this。
可以这样

<script runat="server">
public static int gObject1;
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
gObject1= 0;

}

也可以这样

<object Id="gObject1" Class="System.Int32" RunAt="server" Scope="application"/>
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
//gObject1= 0; //VS的IntelliSense又提示属性是只读的..
Application["gObject1"] = 100;

}
//aspx页面这样访问 

<form id="form1" runat="server">
<div>
<%=Application["gObject1"]%>
</div>
</form>
邀月 | 园豆:25475 (高人七级) | 2009-09-07 14:59
我想知道那个Object如何初始化? MSDN上面说(http://msdn.microsoft.com/en-us/library/47012326(VS.71).aspx) When the ASP.NET processor encounters a server-side <object> tag in an ASP.NET application file, it generates a property on the class, using the id attribute of the tag as the property name. The implicit Get method of the property is then configured to create an object the first time the property is referenced. If the scope attribute of the <object> tag is set to either application or session, a reference to the object (or at least enough information to lazily create it the first time it is accessed) is added to the StaticObjects collection of the appropriate application or to the StaticObjects collection of the appropriate session object. The object is automatically added to the namespace of all .aspx pages within the application. The classid, progid, and class attributes are mutually exclusive. It is an error to use more than one of these attributes within a single server-side <object> tag.
支持(0) 反对(0) 野蛮 | 园豆:165 (初学一级) | 2009-09-08 12:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册