Recipe 18.4 Determining the User s Browser Type

     

Recipe 18.4 Determining the User 's Browser Type

18.4.1 Problem

Your application requires the use of a specific browser and you want to determine whether the required browser is being used before allowing a user to access your application.

18.4.2 Solution

Use the properties of the Request.Browser object to determine the browser type and version, and then take the action required by your application.

In the code-behind class for the page, use the .NET language of your choice to:

  1. Use the Browser property of the Request.Browser object to return a string representing the full browser type, such as "IE" in the case of Internet Explorer.

  2. Use the Version property of the Request.Browser object to return a string that represents the major and minor version of the browser, such as "6.0" in the case of IE 6.0.

  3. Take action accordingly , such as outputting a message indicating whether the user's browser is compatible with the application.

Example 18-10 through Example 18-12 show the .aspx file and the VB and C# code-behind files for an application that demonstrates the solution. The output of our example program is shown in Figure 18-2.

Figure 18-2. Determining the user's browser
figs/ancb_1802.gif

18.4.3 Discussion

With the variety of different browsers in use today and the significant variation in their capabilities, it is not uncommon to need to determine the type of browser being used. Then, based on the browser, you may need to inform the user that the browser is incompatible with your application or output different HTML to support the specific browser.

Our example demonstrates the functionality provided in ASP.NET to determine the browser type and version, and then output this information along with a message indicating whether the user's browser is compatible with the application. The .aspx file contains two asp:Literal controls used to output messages to the user. The first outputs the browser version information, and the second informs the user whether his browser is compatible with the application.

In the Page_Load method of the code-behind, the Browser and Version properties of the Request.Browser object are used to create a message to inform the user of the detected browser version. The Browser property returns a string such as "IE", "Netscape", "Opera", etc. The Version property returns a string that represents the major and minor version of the browser. For Internet Explorer 6.0, for instance, the Browser property will return "IE" and the Version property will return "6.0".

Next, the browser type and major version number are compared to the minimum requirements for the application (IE and Version 5). If the browser is IE 5.0 or later, a message is displayed indicating the browser is compatible with the application. Otherwise, a message is displayed indicating the application requires IE 5.0 or later.

For your application, you might want to check the browser version on the home page. If the browser is compatible with your application, output the home page normally. Otherwise, redirect the user to a message page indicating the browser requirements.

The Request.Browser object contains many properties not used in this example but that may be useful in your application. Table 18-1 lists some of the commonly used properties. For a complete list of the available properties, refer to the documentation on the HttpBrowserCapabilities class in the MSDN Library.

Table 18-1. Commonly used browser object properties

Property

Description

Browser

Returns a string indicating the browser type (IE, Netscape, Opera, etc.)

Cookies

Returns a Boolean value indicating whether the browser supports cookies

JavaScript

Returns a Boolean value indicating whether the browser supports JavaScript

MajorVersion

Returns an integer value indicating the major version of the browser (integer portion of the browser version)

MinorVersion

Returns an double value indicating the minor version of the browser (decimal portion of the browser version)

Version

Returns a string representing the full browser version (integer and decimal portion)


Properties that return Boolean values, such as Cookies and JavaScript , indicate what the browser is capable of supporting but not necessarily the current configuration. If the browser supports cookies but the user has configured the browser to disable cookies, the Cookies property will still return true.


18.4.4 See Also

HttpBrowserCapabilities documentation in the MSDN Library

Example 18-10. Determining the user browser type (.aspx)
 <%@ Page Language="vb" AutoEventWireup="false"           Codebehind="CH18DeterminingBrowserVB.aspx.vb"           Inherits="ASPNetCookbook.VBExamples.CH18DeterminingBrowserVB" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>   <head>     <title>Determining Browser</title>     <link rel="stylesheet" href="css/ASPNetCookbook.css">   </head>   <body leftmargin="0" marginheight="0" marginwidth="0" topmargin="0">     <form id="frmDetermineBrowser" method="post" runat="server" >       <table width="100%" cellpadding="0" cellspacing="0" border="0">         <tr>           <td align="center">             <img src="images/ASPNETCookbookHeading_blue.gif">           </td>         </tr>         <tr>           <td class="dividerLine">             <img src="images/spacer.gif" height="6" border="0"></td>         </tr>       </table>       <table width="90%" align="center" border="0">         <tr>           <td><img src="images/spacer.gif" height="10" border="0"></td>         </tr>         <tr>           <td align="center" class="PageHeading">             Determining User Browser (VB)           </td>         </tr>         <tr>           <td><img src="images/spacer.gif" height="10" border="0"></td>         </tr>         <tr>           <td align="center" class="MenuItem">  <asp:Literal ID="litBrowser" Runat="server" />  <br /><br />  <asp:Literal ID="litMessage" Runat="server" />  </td>         </tr>       </table>     </form>   </body> </html> 

Example 18-11. Determining the user browser type code-behind (.vb)
 Option Explicit On  Option Strict On '----------------------------------------------------------------------------- ' '   Module Name: CH18DeterminingBrowserVB.aspx.vb ' '   Description: This module provides the code behind for the  '                CH18DeterminingBrowserVB.aspx page ' '***************************************************************************** Namespace ASPNetCookbook.VBExamples   Public Class CH18DeterminingBrowserVB     Inherits System.Web.UI.Page     'controls on the form     Protected litBrowser As System.Web.UI.WebControls.Literal     Protected litMessage As System.Web.UI.WebControls.Literal     '*************************************************************************     '     '   ROUTINE: Page_Load     '     '   DESCRIPTION: This routine provides the event handler for the page load     '                event.  It is responsible for initializing the controls      '                on the page.     '-------------------------------------------------------------------------     Private Sub Page_Load(ByVal sender As System.Object, _                           ByVal e As System.EventArgs) Handles MyBase.Load  'output user browser   litBrowser.Text = "Your browser is " & _   Request.Browser.Browser & " " & _   Request.Browser.Version   'check to see if it is an acceptable version   If ((Request.Browser.Browser = "IE") AndAlso _   (Request.Browser.MajorVersion >= 5)) Then   'output message indicating it is OK   litMessage.Text = "It is compatible with this application."   Else   'output message indicating IE 5 or later must be used   litMessage.Text = "This application requires IE 5.0 or later."   End If  End Sub  'Page_Load   End Class  'CH18DeterminingBrowserVB End Namespace 

Example 18-12. Determining the user browser type code-behind (.cs)
 //---------------------------------------------------------------------------- // //   Module Name: CH18DeterminingBrowserCS.aspx.cs // //   Description: This module provides the code behind for the  //                CH18DeterminingBrowserCS.aspx page // using System; namespace ASPNetCookbook.CSExamples {   public class CH18DeterminingBrowserCS : System.Web.UI.Page   {     // controls on the form     protected System.Web.UI.WebControls.Literal litBrowser;     protected System.Web.UI.WebControls.Literal litMessage;          //************************************************************************     //     //   ROUTINE: Page_Load     //     //   DESCRIPTION: This routine provides the event handler for the page      //                load event.  It is responsible for initializing the      //                controls on the page.     //------------------------------------------------------------------------     private void Page_Load(object sender, System.EventArgs e)     {  // output user browser   litBrowser.Text = "Your browser is " +   Request.Browser.Browser + " " +   Request.Browser.Version;   // check to see if it is an acceptable version   if ((Request.Browser.Browser == "IE") &   (Request.Browser.MajorVersion >= 5))   {   // output message indicating it is OK   litMessage.Text = "It is compatible with this application.";   }   else   {   // output message indicating IE 5 or later must be used   litMessage.Text = "This application requires IE 5.0 or later.";   }  }  // Page_Load   }  // CH18DeterminingBrowserCS } 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2006
Pages: 179

Similar book on Amazon

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