May 6, 2011

Device Filtering In Asp.Net

Asp.Net allows customization of properties of web server controls depending on the browser that is rendering the control. Well, this can be done by using javascript and css as well, by detecting the browser using javascript and then applying the correct css styles. Asp.Net makes it very easy to do without creating javascript or css.

The attributes of the controls need to contain the browser name to allow device filtering. For example, to customize the ForeColor property in Internet Explorer, the attribute need to be renamed to ie:ForeColor. For Mozilla, it will be Mozilla:ForeColor and for Pocket Internet Explorer, it’s PIE:ForeColor.

The following is sample code showing the attributes in a Label and TextBox control.

Sampele code
  1. <asp:Label ID="L1" runat="server" ie:Text="Internet explorer" Mozilla:Text="mozilla" ie:BackColor="Red" Mozilla:BackColor="Green" ie:ForeColor="White"></asp:Label>
  2. <asp:TextBox ID="T3" runat="server" ie:Text="IE" Mozilla:Text="mozilla" ie:BackColor="Gray" Mozilla:BackColor="Black" ForeColor="White"></asp:TextBox>

As can be seen, even the Text property can be customized. That is, the text on the controls would be different depending on the browser.

One important to note is that these properties can also be set declaratively meaning the properties cannot be accessed and / or modified from code behind.

0 comments: