Server-Side Object Tags — Global.asax
The <object> tag can be used to declare and create new application and session variables using a declarative, tag-based syntax. These are loaded when your application loads and are available for use by your application. The <object> tag can take three different forms, depending on the reference type for the object being created.
<object id="id" runat="server" scope="scope" class="Class Name">
<object id="id" runat="server" scope="scope" progid="COM ProgID"/>
<object id="id" runat="server" scope="scope" classid="COM ClassID"/>
Attributes
The <object> tag supports the following attributes.
- id
Provides a unique identifier to use when declaring an object on the page. - runat
Must be set to server for the object to execute within ASP.NET. All non-server values cause the page compiler to assume that the <object> tag should be sent to the client to handle. - scope
Declares the scope at which the object is declared. pipeline indicates that the object is available only to the HTTP request for the containing ASP.NET application file. application indicates that the object is populated into the HttpApplicationState collection. session indicates that the object is populated into HttpSessionState. If no scope is specified, the scope attribute defaults to pipeline. - class
Specifies the .NET Framework class to instantiate. - progID
Specifies the COM component to instantiate by specifying the component's programmatic identifier. - classID
Specifies the COM component to instantiate using the component's class identifier.
Parameters
- id
A unique name to use when referencing the object in subsequent code. - Class Name
The name of the class to instantiate. - COM ProgID
The COM program ID of the component to instantiate. - COM ClassID
The COM class ID of the component to instantiate.
Remarks
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.
Example
The following example demonstrates how to use the <object> tag to create an ArrayList object on a Web Forms page. You can programmatically access the object by using the items value of its id attribute.
<object id="items" class="System.Collections.ArrayList" runat="server"/>