Signup · Login
Stardeveloper.com  
Home · Articles · Forums · Advertise · Contact
Search this Website
Stay Informed
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (29)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Product Reviews (1)
  Applets (1)
  Patterns (1)
Latest Forum Activity
Re: Preventing/Investigating the Source of SQ..
by Faisal Khan on 25 Nov 2008 Go To Post

Re: Column multiplication..
by Faisal Khan on 24 Nov 2008 Go To Post

Re: Unable to insert data in an Access databa..
by Faisal Khan on 12 Nov 2008 Go To Post

Re: HOW TO MOVE FROM APACHE TO MS IIS
by Faisal Khan on 12 Nov 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by peeru999 on 8 Oct 2008 Go To Post

Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Browser Capabilities Component in ASP.NET
 
Browser Capabilities Component in ASP.NET
by Faisal Khan.

Overview
In this article we are going to learn about the HttpBrowserCapabilities component of ASP.NET which will allow you to detect the user browser and develop browser friendly pages.

HttpBrowserCapabilites Component
HttpBrowserCapabilities is a class present in the System.Web namespace. To use it you don't have to create new instance of this class, simply use the one provided by Page.Request.Browser property within the ASP.NET page.

To see the complete listing of constructors and properties of HttpBrowserCapabilities class, use the following link :

Coding the ASP.NET page
To demonstrate the different properties of HttpBrowserCapabilities class, we'll create an ASP.NET page. Create a new .aspx page and save it as browserCaps.aspx in any directory from where you can run ASP.NET pages. Now copy and paste the following code in it :

<%@ Page Language="C#" %>
<html>
<head>
	<style>
	p { font-family:Tahoma, Sans-Serif; font-size:10pt; }
	td { font-family:Tahoma, Sans-Serif; font-size:8pt; padding-left:10; }
	tr { background-color:#F7F7F7; }
	.title { color:#396B9C; font-weight:bold; }
	.header { background-color:#CCDDEE; }
	</style>
</head>
<body>

	<!-- p class="title">Browser Capabilities :</p -->
	
	<table width="90%" border="0" align="center" 
		cellpadding="2" cellspacing="2">
		<tr class="header">
			<td width="30%">Property</td>
			<td>Value</td>
		</tr>
		<tr>
			<td>ActiveXControls </td>
			<td> <%= Request.Browser.ActiveXControls %></td>
		</tr>
		<tr>
			<td>AOL </td>
			<td> <%= Request.Browser.AOL %></td>
		</tr>
		<tr>
			<td>BackgroundSounds </td>
			<td> <%= Request.Browser.BackgroundSounds %></td>
		</tr>
		<tr>
			<td>Beta </td>
			<td> <%= Request.Browser.Beta %></td>
		</tr>
		<tr>
			<td>Browser </td>
			<td> <%= Request.Browser.Browser %></td>
		</tr>
		<tr>
			<td>CDF </td>
			<td> <%= Request.Browser.CDF %></td>
		</tr>
		<tr>
			<td>ClrVersion </td>
			<td> <%= Request.Browser.ClrVersion %></td>
		</tr>
		<tr>
			<td>Cookies </td>
			<td> <%= Request.Browser.Cookies %></td>
		</tr>
		<tr>
			<td>Crawler </td>
			<td> <%= Request.Browser.Crawler %></td>
		</tr>
		<tr>
			<td>EcmaScriptVersion </td>
			<td> <%= Request.Browser.EcmaScriptVersion %></td>
		</tr>
		<tr>
			<td>Frames </td>
			<td> <%= Request.Browser.Frames %></td>
		</tr>
		<tr>
			<td>JavaApplets </td>
			<td> <%= Request.Browser.JavaApplets %></td>
		</tr>
		<tr>
			<td>JavaScript </td>
			<td> <%= Request.Browser.JavaScript %></td>
		</tr>
		<tr>
			<td>MajorVersion </td>
			<td> <%= Request.Browser.MajorVersion %></td>
		</tr>
		<tr>
			<td>MinorVersion </td>
			<td> <%= Request.Browser.MinorVersion %></td>
		</tr>
		<tr>
			<td>MSDomVersion </td>
			<td> <%= Request.Browser.MSDomVersion %></td>
		</tr>
		<tr>
			<td>Platform </td>
			<td> <%= Request.Browser.Platform %></td>
		</tr>
		<tr>
			<td>Tables </td>
			<td> <%= Request.Browser.Tables %></td>
		</tr>
		<tr>
			<td>Type </td>
			<td> <%= Request.Browser.Type %></td>
		</tr>
		<tr>
			<td>VBScript </td>
			<td> <%= Request.Browser.VBScript %></td>
		</tr>
		<tr>
			<td>Version </td>
			<td> <%= Request.Browser.Version %></td>
		</tr>
		<tr>
			<td>W3CDomVersion </td>
			<td> <%= Request.Browser.W3CDomVersion %></td>
		</tr>
		<tr>
			<td>Win16 </td>
			<td> <%= Request.Browser.Win16 %></td>
		</tr>
		<tr>
			<td>Win32 </td>
			<td> <%= Request.Browser.Win32 %></td>
		</tr>
	<%
		b = null;
	%>
	</table>

</body>
</html>

browserCaps.aspx Code Explanation
The first line tells that the language we are going to use on our ASP.NET page is C#.

<%@ Page Language="C#" %>

Next we display the values of all the properties of HttpBrowserCapabilites class by simply writing them to the user scren using Response.Write() method. The complete path to get a property of HttpBrowserCapabilites class is actually Page.Request.Browser.PropertyName where PropertyName can be any of the properties of HttpBrowserCapabilites class. But we can use a shortcut like the one we used in our browserCaps.aspx page i.e. Request.Browser.PropertyName, because when using Request object, it is assumed Page.Request.

Developing Browser Friendly Pages
Property names of HttpBrowserCapabilites class are quite suggestive of what they tell. You can for example use Crawler property to determine if the client is a search engine crawler or not, and if it is then show some more search engine specifc content.

If your site uses JavaScript, CSS, DHTML etc, you can get info about the browser if it supports these features or not by looking at JavaScript, Browser and Version properties. Internet Explorer and Netscape browsers are represented as "IE" and "Netscape" in the Browser property respectively. Version 4.0 and above of these browsers support many of the features listed above, but Netscape 6 is far better than Netscape 4.x when it comes to rendering CSS rich web pages. You can determine if the client is using Netscape 6 by using a C# code like following :

if(Request.Browser.Browser.Equals("Netscape") &&
   Request.Browser.MajorVersion >= 6)
      Response.Write("Thank God! you are using a better version of Netscape.");

Above code will display a "Thanks God!" message to Netscape 6 users only. MajorVersion property returns a 32 bit Integer ( Int32 ) so we can easily use ">" and "=" operators to check for the correct version.

Following code displays "Yours is the best!" message to Internet Explorer 5.x and above browsers :

if(Request.Browser.Browser.Equals("IE") && Request.Browser.MajorVersion >= 5)
	Response.Write("Yours is the best!");

Hope you've understood how to display different content to different browsers by looking at the properties of HttpBrowserCapabilites class. Now run the browserCaps.aspx page in your browser and notice the different values you get. Following image shows how browserCaps.aspx appeared in my Internet Explorer 5.5 browser :

browserCaps.aspx
browserCaps.aspx

 ( No Further Pages )

Related Articles
  1. File Uploading in ASP.NET Using C#
  2. Sending e-mail with attachments from an ASP.NET page
  3. Your first ASP.NET page
  4. Sending E-Mails using ASP.NET
  5. A Preview of Active Server Pages+
  6. Exposing Web Services
  7. Get Detailed Information About Your Site Visitors In Real Time using ASP.NET
  8. ASP.NET Website Programming: Problem - Design - Solution : Deploying the Site
  9. How to determine what server is given web site running on using ASP.NET?
  10. Sending Mass E-Mails using ASP.NET

Comments/Questions ( Threads: 1, Comments: 2 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Another Kahn Masterpiece but Short-Sighted... ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2008 Stardeveloper.com, All Rights Reserved.