avr. 23

Some explanation 

Depending of the browser, some control may need to be rendered differently. By default the .NET framework works with "Control Adapters", id est, some classes that will be plugged into the rendering process.

How does it works ? When the server receives a request, it will try to find which browser does the request, based on the user agent or on HTTP headers for example. To do it rely on some browser definition files (of extension .browser). To see them, please go on %windir%/Microsoft.NET/Framework/<version>/CONFIG/Browsers.

When the server has identified the browser (at worse, it would be identified as a "Default" browser), and that it needs to render a control, it will look if there is some control adapters plugged for this kind of control. If there are some, they will be in charge of the rendering. If not, the control will render himself. Note that an adapter can redirect / delegate some treatment to the control.

Creating a basic Adapter for TextBoxes

  • Let's create a solution with a website and a "Web Control Class Library" (named "MyControlLibrary")
  • Let's add a new class in the library for the adapter. Note that by convention, an adapter will always be named "ControlNameAdapter" where "ControlName" is the control we want to adapt. Here we'll so create the "TextBoxAdapter"

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.Adapters;

 

namespace MyControlLibrary

{

   public class TextBoxAdapter : WebControlAdapter

   {

      /// <summary>

      /// If you need to access the adapted control, the framework advice to expose

      /// a private method encapsulating the Adapter's Control property.

      ///

      /// We can cast it without doing any test, as by naming convention,

      /// this adapter shall only be used for adapting Textboxes (or derived types)

      /// </summary>

      private TextBox Control

      {

         get { return (TextBox)base.Control; }

      }

 

      /// <summary>

      /// Let's add some extra attribute in our control's begin tag

      /// </summary>

      protected override void RenderBeginTag(HtmlTextWriter writer)

      {

         writer.AddAttribute("MyNewAttribute", "This has been set in the adapter");

         writer.AddAttribute("ControlType", this.Control.GetType().FullName);

 

         base.RenderBeginTag(writer);

      }

   }

}

How to use it in a website ?

The mapping between a control and its adapter is done at the website level, using the browser definitions.

  • In the website, add the special folder "App_Browsers"
  • In the folder add a new browser definition file

We have several solutions here. The simplest is to add some capabilities to an existing browser. We can either decide to do this for all browser, or for a specific browser.

<browsers>

<!--

   Here we extend the "Default" browser (ie the basis browser from which all other

   browser derive (see in %windir%/Microsoft.NET/Framework/<Version>/CONFIG/Browsers)

 

   To extend only a specific browser, replace the refID by another one eg :

      refID="IE" for all versions of IE

      or refID="IE50" for only the 5.0 version

   -->

   <browser refID="Default">

      <controlAdapters>

         <adapter adapterType="MyControlLibrary.TextBoxAdapter"

                  controlType="System.Web.UI.WebControls.TextBox" />

      </controlAdapters>

   </browser>

</browsers>

How to use this functionality for testing purposes ?

Let's explain my context : in a TDD environment all code (and so pages) are tested before being written. To do so, we can use for instance basic HTTP request that will call my page and then validate them using any HTML parsing. In order to facilitate the testing, we may want to base our research on some extra tags. That's where Control Adapters may enter in the game. As a consequence we want some control adapters to be used only for testing. The simplest way to do so is to consider that our testing framework will use a "special browser" by giving some clues in the HTTP Headers or in the user agent.

For example by using this code, we could make request by simulating a IE 5.0 browser, and sending an extra hint.

HttpWebRequest request = HttpWebRequest.Create("http://localhost/MyPage.aspx")

   as HttpWebRequest;

request.UserAgent

   = "Mozilla/4.0 (compatible; MSIE 5.0; Windows 95; MyTestSimulator)";

At the end, we could update our browser file to have this kind of definition :

<browsers>

   <!-- Let's create a new browser, extending another one,

   eg IE50, Mozilla, MozillaFirefox, Default, ... -->

   <browser id="MyTestSimulator" parentID="IE50">

      <!-- Let's here base our research on our user agent hint to match our

      "special test browser"

      -->

      <identification>

         <userAgent match="MyTestSimulator" />

      </identification>

 

      <controlAdapters>

         <adapter adapterType="MyControlLibrary.TextBoxAdapter"

                  controlType="System.Web.UI.WebControls.TextBox" />

      </controlAdapters>

   </browser>

</browsers>

Tags: | | | |

Commentaires

Ajouter un commentaire




biuquote
  • Commentaire
  • Aperçu immédiat
Loading