Accessing the Current Context


You can access application and session state, server variables , Response and Request objects, and security information through the Context property. For example, the following control displays the values of the UserHostAddress and UserAgent properties retrieved from the Request object.

Listing 28.34 ControlContext.vb
 Imports System Imports System.Web Imports System.Web.UI Namespace myControls Public Class ControlContext Inherits Control Overrides Protected Sub Render( objTextWriter As HtmlTextWriter )   Dim strHostAddress As String   Dim strUserAgent As String   strHostAddress = Context.Request.UserHostAddress   strUserAgent = Context.Request.UserAgent   objTextWriter.WriteLine( "<li> HostAddress: " & strHostAddress )   objTextWriter.WriteLine( "<li> UserAgent: " & strUserAgent ) End Sub End Class End Namespace 

The C# version of this code can be found on the CD-ROM.

In Listing 28.34, the values of the UserHostAddress and UserAgent properties are displayed in the Render method. Both properties are retrieved through the Context property of the Control class.

You also can use the Context property to retrieve information about the capabilities of the browser making the request. For example, the control in Listing 28.35 displays the browser type, the browser version, and whether the browser supports JavaScript.

Listing 28.35 ControlCaps.vb
 Imports System Imports System.Web Imports System.Web.UI Namespace myControls Public Class ControlCaps Inherits Control Overrides Protected Sub Render( objTextWriter As HtmlTextWriter )   Dim strBrowserType As String   Dim strVersion As String   Dim blnJavascript As Boolean   strBrowserType = Context.Request.Browser.Browser   strVersion = Context.Request.Browser.Version   blnJavascript = Context.Request.Browser.Javascript   objTextWriter.WriteLine( "<li> Browser: " & strBrowserType )   objTextWriter.WriteLine( "<li> Version: " & strVersion )   objTextWriter.WriteLine( "<li> Javascript: " & blnJavascript.ToString() ) End Sub End Class End Namespace 

The C# version of this code can be found on the CD-ROM.

In Listing 28.35, the Context property retrieves an instance of the Browser object, which contains properties that represent the browser capabilities.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net