How to set the properties of ASP.NET Server controls for compatibility with different browsers

8:50:00 pm 0 Comments


This is a small but useful trick that I recently came across.
We can set the properties of a server control according to the browser without writing a single line of javascript.
 
Let's check out some code samples:
 
Label control:
 
<asp:Label ID="lblTest" runat="server" ie:CssClass="IEStyle" mozilla:CssClass="FFStyle" CssClass="DefaultStyle" ie:Text="You are in Internet explorer." mozilla:Text="You are in Firefox." Text="You are in other browser." />
 
If we will define css classes like:
.IEStyle{color:Red;}
.FFStyle{color:Blue;}
.DefaultStyle{color:Black;}
 
Output:
IE  : You are in Internet explorer.
FF : You are in Firefox.
Others : You are in other browser.
 
TextBox Control:

<asp:TextBox ID="txtBoxTest" runat="server" ie:Text="You are in Internet explorer." mozilla:Text="You are in Firefox." Text="You are in other browser." ie:CssClass="IEStyle" mozilla:CssClass="FFStyle" CssClass="DefaultStyle" />
  
Output:
Same behaviour as of above label.
 
Button Control:

<asp:Button ID="btnTest" runat="server" ie:Text="Submit [IE]" mozilla:Text="Submit [FF]" Text="Submit [Others]" ie:OnClientClick="javascript:alert('You are in Internet explorer.');" mozilla:OnClientClick="javascript:alert('You are in Firefox!');" OnClientClick="javascript:alert('You are in other browser.');" ie:CssClass="IEStyle" mozilla:CssClass="FFStyle" CssClass="DefaultStyle" />
 
If we will define css classes like:
.IEStyle{color:Red;}
.FFStyle{color:Blue;}
.DefaultStyle{color:Black;}
 
Output :
IE  : Button with text " Submit [IE] ". By clicking that button one alert will appear which will say : "You are in Internet explorer."
FF : Button with text " Submit [FF] ". By clicking that button one alert will appear which will say : "You are in Firefox."
Others : Button with text " Submit [Others] ". By clicking that button one alert will appear which will say : "You are in other browser."
 
NOTE : There is no intellisense available for above in Visual Studio.

0 comments: