Isn't it great when you work with a tool day after day and you thought you knew everything there was to know about it? Then find out something that has been there for a long time that you somehow missed? Here's two things that have been in ASP.NET since version 2.0 that I somehow missed until just recently.
...as if there are only 2? Hehe. I am sure there are many more, but I was actually surprised when I came across these items. They're simple enough that I could not believe that I missed them before. I'm sure that I'm not the only one who missed these items. The fact that I hadn't heard about them indicates that they've not been blogged about heavily or that obvious in the docs (if at all?).
Setting Server Control Properties Based on Target Browser
This one was surprising. I came across this one on John Katsiotis' Web Dev & Stuff
. The fact that I've written code so many times to accomplish this, when it's apparently been built in since version 2.0 just kills me. The properties of server controls will let you specify a browser filter to apply the property value for. That is, you can set a different value for a property for IE than for Firefox - and for anything else. I didn't try all properties, but I did try many that would matter, such as Text and OnClientClick etc, and they do work. Take a look at the following markup:
<asp:Label runat="server" ID="labelText"
ie:Text="This is IE text"
mozilla:Text="This is Firefox text"
Text="This is general text"
<asp:Button runat="server" ID="buttonText"
Notice the "ie:" and "mozilla:" prefixes on the Text and OnClientClick properties. The results? Take a look. The following was the result in Firefox:
In Internet Explorer:
In Opera (or anything other than IE or Firefox since I've defined properties for those)
Wow. Pretty cool. Now, I am not saying that this will apply in all cases. It doesn't seem to differentiate between IE7 and IE6, so there are some big limitations. However, this is quite a cool trick and one that will without question come in handy.
I posted a follow up that shows a lot more of the capabilities of using these device filters to target specific browsers in ASP.NET.
Taking an ASP.NET Site Offline with a Message
This was another one that I was literally beside myself that I didn't know this one before. A fellow business partner of mine named James Sutton mentioned this one to me. This one's been around since ASP.NET 2.0 as well. If you have an ASP.NET web application site, and you place a text file named "app_offline.htm" in the root of the site, all requests to that website will redirect to that app_offline.htm file
. Basically, if you need to take an entire ASP.NET site offline, you can place some nice message in that file. Then, any new requests to a URL, any URL, in that website will redirect to that file allowing you to do maintenance to the site, upgrades, or whatever. It is not really a redirect though. ASP.NET essentially shuts down the site, unloads it from the server, and stops processing any requests to that site. That is, until you delete the app_offline.htm file - then things will continue as normal and your ASP.NET site will load up and start serving requests again.
A super-cool side effect of this is that any files that are locked by the site, such as a database or other resources, are freed since the application domain has been unloaded from the server. This allows you to remove the locks from those files and replace them, without the need to do a full IISRESET, taking down other sites on the server. One thing to keep in mind with this file however, make sure you out enough content in it so it is larger than 512 bytes or IE will consider it a 404 and will display the 404 instead of the contents of your app_offline.htm file.
I love coming across new tricks like this, but it does make me wonder, why didn't I know about these before?