DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 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. In this tutorial, we reference the Browser Object to display information about both the Client and the Browser. This could help us determine how our users see our page, depending upon their settings. These could also help us display our page differently so that our users get the most out of the site, no matter what their set-up.
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:

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

<form id="form1" runat="server">
<div>
Browser Capabilities:<br />
<table style="width: 95%; position: static; text-align: left">
<tr>
<td style="vertical-align: top; width: 30%; text-align: right">
ActiveX Controls:</td>
<td style="vertical-align: top; width: 70%; text-align: left">
<asp:Label ID="Label1" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="vertical-align: top; width: 30%; text-align: right">
AOL:</td>
<td style="vertical-align: top; width: 70%; text-align: left">
<asp:Label ID="Label2" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="vertical-align: top; width: 30%; text-align: right">
Background Sounds:</td>
<td style="vertical-align: top; width: 70%; text-align: left">
<asp:Label ID="Label3" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="vertical-align: top; width: 30%; text-align: right">
CDF:</td>
<td style="vertical-align: top; width: 70%; text-align: left">
<asp:Label ID="Label4" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="vertical-align: top; width: 30%; text-align: right">
.NET Framework:</td>
<td style="vertical-align: top; width: 70%; text-align: left">
<asp:Label ID="Label5" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="vertical-align: top; width: 30%; text-align: right">
Cookies:</td>
<td style="vertical-align: top; width: 70%; text-align: left">
<asp:Label ID="Label6" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="vertical-align: top; width: 30%; text-align: right">
ECMA:</td>
<td style="vertical-align: top; width: 70%; text-align: left">
<asp:Label ID="Label7" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="vertical-align: top; width: 30%; text-align: right">
HTML Frames:</td>
<td style="vertical-align: top; width: 70%; text-align: left">
<asp:Label ID="Label8" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="vertical-align: top; width: 30%; text-align: right">
Java Applets:</td>
<td style="vertical-align: top; width: 70%; text-align: left">
<asp:Label ID="Label9" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="vertical-align: top; width: 30%; text-align: right">
Browser Version:</td>
<td style="vertical-align: top; width: 70%; text-align: left">
<asp:Label ID="Label10" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="vertical-align: top; width: 30%; text-align: right">
Broswer Platform:</td>
<td style="vertical-align: top; width: 70%; text-align: left">
<asp:Label ID="Label11" runat="server"></asp:Label></td>
</tr>
<tr>
<td style="vertical-align: top; width: 30%; text-align: right">
</td>
<td style="vertical-align: top; width: 70%; text-align: left">
</td>
</tr>
</table>

<br />
 </div>
</form>

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.
The code-behind should look something like this:

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;
}
}


We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!