Vous êtes sur la page 1sur 4

ASP.

NET Web Server Controls and


Browser Capabilities
Other Versions

.NET Framework 4
Visual Studio 2008
.NET Framework 3.0
Visual Studio 2005
Different browsers, and different versions of the same browser, support different
features. ASP.NET server controls will automatically determine the browser that
has requested the page and render the appropriate markup for that browser.
However, some control features cannot be rendered on older browsers, so it is a
good idea to look at the output of your pages on as many browser types as you
can to make sure that the pages are presented to all browsers the way you want
them to be.

Detecting Browser Types


By default, ASP.NET determines browser capabilities by reading the user-agent
information that is passed from the browser to the server during a request. It
compares the user-agent string that is received from the browser to user agent
strings that are stored in browser definition files. These browser definition files
contain information about the capabilities of various user agents. When ASP.NET
finds a match between the current user-agent string and a user-agent string in a
browser definition file, it loads the corresponding browser capabilities into
the HttpBrowserCapabilities object. The properties of
theHttpBrowserCapabilities object can then be used to determine whether the
browser type that is represented by the user agent supports scripting, styles,
frames, and so on. Based on these capabilities, the controls on the page render
Web controls using appropriate markup.

Note

Browser capabilities indicate whether the browser type in general supports features such as
JavaScript, not whether an individual instance of the browser has these features enabled or
disabled.

The HttpBrowserCapabilities object is available in the Browser property of


the HttpRequest object. You can access information about some of the browser
capabilities in strongly-typed properties of theHttpBrowserCapabilities object.
Other capabilities are defined only in the browser definition files and are
available only as entries in a dictionary collection.
For example, to determine whether the type and version of the current browser
supports a particular version of JavaScript, you can use a property that is defined
only in the browser definition files (not inHttpBrowserCapabilitiesBase), as
shown in the following example:
C#
VB
string jsVersion = Request.Browser["JavaScriptVersion"];

If the browser that is making the request is Internet Explorer 8,


the jsVersion string will contain the value "1.5".
To determine whether the request originated from a mobile device, you can use
the IsMobileDevice property, as shown in the following example:
C#
VB

bool isMobile = Request.Browser.IsMobileDevice;

ASP.NET stores the default browser definition files in the following folder:
%SystemRoot%\Microsoft.NET\Framework\versionNumber\Config\Browsers
Note

The browser definition files were updated for ASP.NET 4, and the new versions are not
backward compatible with browser definition files that were provided with earlier versions
of ASP.NET For more information, see How to: Upgrade an ASP.NET Web Application to

ASP.NET 4.

Defining Custom Browser Types


You can define custom browser types by creating new browser definition files
and adding them to the ASP.NET Browsers folder or to the App_Browsers folder
of a Web site. If you update or add browser definition files to the ASP.NET
Browsers folder, you must run the aspnet_regbrowsers.exe tool.
Note

The browserCaps section of configuration files (.config) is deprecated. For backward


compatibility with ASP.NET 1.1, the configuration settings in the browserCaps section are
still effective if they are set at the application level. However, the settings are merged with
the information from the machine-level browser definition files and any application-level
App_Browser folders. For more information, seeBrowser Definition File Schema
(browsers Element).

Overriding Browser-Type Detection


If you want to explicitly control how the page is rendered instead of relying on
automatic browser detection, you can set the page's ClientTarget property. You
can set the property declaratively as an attribute of the @ Page directive for that
page, or you can set it programmatically.
The value of the ClientTarget property is an alias for the type of browser you
want to render the page for. For example, if you want to see how a page would
render on Apple iPhone without using an emulator, you could create an alias for
iPhone and set the ClientTarget property to that alias. The alias that you specify
must be defined in the clientTarget section of the configuration file. The
predefined defaults areuplevel and downlevel.

You can create additional aliases by defining them in the root Web.config file or
in the application Web.config file. For more information, see clientTarget
Element (ASP.NET Settings Schema).

Ajax-Enabled ASP.NET Controls and Features


Ajax-enabled features in ASP.NET are compatible with most modern browsers
and run with the default security settings for these browsers. These controls and
features require that the browser be able to run client script.
The UpdatePanel and ScriptManager controls are examples of Ajax-enabled
controls. For a list of compatible browsers and of recommended security settings
for Ajax-enabled controls and features, see Browser Security Settings for Ajax
Enabled ASP.NET Pages.
Note

You can use the properties exposed by the HttpBrowserCapabilities object to determine
whether a browser type supports ECMAScript (JScript, JavaScript). However,
the HttpBrowserCapabilities object does not report whether JavaScript is enabled for a
particular instance of a browser type.

For an overview of Ajax features in ASP.NET, see Microsoft Ajax.

Client Script
Some functionality of ASP.NET server controls depends on being able to run
client script. The client script is automatically generated and sent as part of the
page, if the browser is capable of executing script. Even so, some users might
have turned off script execution in their browsers, and will therefore not be able
to fully use the control's capabilities. For more information, see Client Script in
ASP.NET Web Pages.

Vous aimerez peut-être aussi