Monday, September 13, 2010

Browser Capabilities - Using the Browser Object in C#

This tutorial will show how to reference the Browser object to retrieve information about what your client can display. C# version.
The text attribute of each label in the ASPX page will be dynamically changed upon runtime, depending upon the user configuration / browser.
First, let's create a layout to display the information:


Browser Capabilities:














ActiveX Controls:


AOL:


Background Sounds:


CDF:


.NET Framework:


Cookies:


ECMA:


HTML Frames:


Java Applets:


Browser Version:


Broswer Platform:









The following code checks for values for specific properties of the Browser Object.
We are able to output these values to the user to notify them, but we are also able to use these values in the code to display our page differently to different users. For example, not using ActiveX Controls if the user's browser does not support ActiveX.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Browser.ActiveXControls == true)
{
Label1.Text = "This browser supports ActiveX Controls.";
}
else
{
Label1.Text = "This browser does not support ActiveX controls.";
}

if (Request.Browser.AOL == true)
{
Label2.Text = "This browser is AOL.";
}
else
{
Label2.Text = "This browser is not AOL.";
}

if (Request.Browser.BackgroundSounds == true)
{
Label3.Text = "This browser supports Background Sounds.";
}
else
{
Label3.Text = "This browser does not support Background Sounds.";
}

if (Request.Browser.CDF == true)
{
Label4.Text = "This browser supports Channel Definition Format.";
}
else
{
Label4.Text = "This browser does not support Channel Definition Format.";
}

if (Request.Browser.ClrVersion.ToString() != "0.0")
{
Label5.Text = "This client supports the .NET Framework! You are running version " + Request.Browser.ClrVersion.ToString();
}
else
{
Label5.Text = "You do not have the .NET Framework installed on your machine.";
}

if (Request.Browser.Cookies == true)
{
Label6.Text = "This browser supports Cookies.";
}
else
{
Label6.Text = "This browser does not currently support Cookies.";
}

Label7.Text = "You are running version " + Request.Browser.EcmaScriptVersion.ToString() + " of ECMA Script.";

if (Request.Browser.Frames == true)
{
Label8.Text = "This browser supports HTML Frames.";
}
else
{
Label8.Text = "This browser does not support HTML Frames.";
}

if (Request.Browser.JavaApplets == true)
{
Label9.Text = "This browser supports Java Applets.";
}
else
{
Label9.Text = "This browser does not support Java Applets.";
}

Label10.Text = Request.Browser.Type.ToString() + "." + Request.Browser.MinorVersion.ToString();

Label11.Text = Request.Browser.Platform;
}
}



No comments:

Post a Comment