Tag Archives: asp.net

Don’t do iisreset do …

a mini batch file in your start menu, when you’re debugging the application startup of a .Net application, and keep wanting to restart it:

taskkill /im aspnet_wp.exe /f
if not errorlevel == 0 pause

or

taskkill /im w3wp.exe /f
if not errorlevel == 0 pause

which does the job much faster.

Assuming, of course, that your reason for wanting to do an iis reset was to force a .Net application restart. And you don’t mind forcing all running .Net applications to restart. And you’re not on a busy production machine at rushhour.

Asp.Net Required Field Validator for a DropDownList

A required field seems a bit odd for a dropdown list because it’s got a value whether you like it or not. But when you feel that your dropdown list should have a required value it’s usually because, you’ve given it an initial value of “–please select–” or somesuch and you want to validate that a ‘real’ value has been chosen.

Easy. Set the InitialValue property:

<asp:requiredfieldvalidator ControlToValidate="ddlMyPickList" 
    Text="*" InitialValue="-1"
    ErrorMessage="You must choose a value"
    runat="server" ID="rfvDdlMyPickList"/>

Specifically, set it to the Value (not the Text) of the ‘please select’ item you added.

Prevent an Asp.Net Button from triggering a Postback

Sometimes, you want an Asp.net Button to not trigger a postback.

One solution is, don’t use an asp.net button, use an html input or link tag instead. However, you may want to use an asp:XXXButton for consistency with the rest of your page; or it may seem a simple way to make the text on the button localisable (although you can equally achieve that on a plain old html control if you give it an id, a runat=”server” and probably a meta:resourcekey); or you may have other stuff you want to do with it serverside.

To stop an asp.net button causing a postback, do it like this:

<asp:LinkButton id="btnX" OnClientClick="return false;" runat="server"/>

The more likely scenarios is, that you want to run clientside javascript. In that case, put “;return false;” after your javascript call:

<asp:LinkButton id="btnX" OnClientClick="functionToCall();return false;" runat="server"/>

Localising programmatically in asp.net code behind

The usual examples for asp.net localisation are for the aspx markup page, not for the code behind.

In fact localisation, accessing localisation resource files programmatically, in code behind is also dead simple. You use GetLocalResourceObject() like so:

ctrl.Text = (string)GetLocalResourceObject("myCtrlKey.Text");
ctrl.AnotherAttribute = (string)GetLocalResourceObject("myCtrlKey.AnotherAttribute");

Using LocalResource means that for a page called MyPage.aspx, you have created a resource file called MyPage.aspx.resx and/or MyPage.aspx.{culturename}.resx in the special directory App_LocalResource.

If you like GlobalResources instead of local, use the special directory App_GlobalResource
to hold a resource file called MyResourceFileName.resx and call:

ctrl.Text= (string)GetGlobalResourceObject("MyResourceFileName", "myGlobalKey");

as per http://msdn.microsoft.com/en-us/library/ms227982.aspx

For a bit more of an overview of asp.net localisation, the page at http://msdn.microsoft.com/en-us/library/ms227427.aspx should only take you a few minutes to scan.

Switching between Visual Studio .Net and Mono with MonoDevelop

For reasons best not examined too closely I switch between between Mac and PC which, since I earn my crust largely with .Net development, means switching between Visual Studio and MS.Net and MonoDevelop with Mono.

Mono is very impressive, it is not at all a half hearted effort, and it does some stuff that MS haven’t done. But when switching environments, there’s always the occasional gotcha. Here are some that have got me, and some solutions.

Gotcha: Linq Expressions don’t work on Mono

Solution: Add a reference to System.Core to your project.

Question: What version of NUnit is built in to mono?

As of Feb 2011, it’s nunit 2.4.8.