Accessing the Aspnet Intrinsics

The Control class has a Context property that enables a server control to access ASP.NET intrinsic objects such as

Application, Session, Request, Response, Error, and Cache.

The Context property is defined as the type System.Web.HttpContext, which is the same type as the Page.Context property, which means it is used in the same way (for example, for tracing, etc.).

The following code shows a simple server control that will output HTML to display the keys and values held in the application and session intrinsics:

using System;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WroxControls {

public class IntrinsicDisplay : WebControl {

protected override void Render(HtmlTextWriter writer) {

writer.Write("<H3>Application Variables</H3>");

foreach( string key in Context.Application )

writer.Write( "<BR>Key = '" + key + "' , Value = '" + Context.Application[key] + "'" );

writer.Write("<H3>Session Variables</H3>");

foreach( string key in Context.Session ) {

writer.Write( "<BR>Key = '" + key + "' , Value = '" + Context.Session[key] + "'" );

Assuming that we have an ASP.NET page that sets some application and session variables in the Page_Load event and declares an instance of this control:

<%@ Register TagPrefix="Wrox" Namespace="WroxControls"

Assembly="Intrinsic" %>

<script runat="server" language="VB">

Sub Page_Load( sender as object, args as EventArgs )

Application("Name") = "Richard"

Application("Age") = "27"

Session("Year") = "1972"

End Sub

<Wrox:IntrinsicDisplay runat="server" />

Our control would render the following output:

0 0

Post a comment