For input validation I often use the Validators provided by Microsoft out of the box: RequiredFieldValidator to check whether user entered some data into the textbox, RangeValidator to check the range of numeric values or dates, RegularExpressionValidator to check the size of user input (and much more of course), CompareValidator to check dates, and today I had to use CustomValidator to check one field depending on another field.
My asp.net application contains a textbox which has to be filled if the user selected ‘Yes’ on a radiobutton. The RequiredFieldValidator does not help because you could not add a condition there. So it has to be done with the CustomValidator. I’ve searched around and finally combined several sources into this solution so here is a complete overview how to set up.
Read the rest of this entry
For input validation I often use the Validators provided by Microsoft out of the box: RequiredFieldValidator to check whether user entered some data into the textbox, RangeValidator to check the range of numeric values or dates, RegularExpressionValidator to check the size of user input (and much more of course), CompareValidator to check dates, and today I had to use CustomValidator to check one field depending on another field.
My asp.net application contains a textbox which has to be filled if the user selected 'Yes' on a radiobutton. The RequiredFieldValidator does not help because you could not add a condition there. So it has to be done with the CustomValidator. I've searched around and finally combined several sources into this solution so here is a complete overview how to set up.
Have a look at my RadioButton. Nothing special here.
<asp:RadioButtonList ID="MyBtn" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="Yes" />
<asp:ListItem Value="No" />
</asp:RadioButtonList></td>
As I want to do the validation in the client we have to do some Javascript here.
<script type="text/javascript">
function validateMyBtn(oSrc, args){
var rbtnValue = null;
var rbtnList = document.getElementsByName('<%= MyBtn.ClientID %>');
var radio = rbtnList[0].getElementsByTagName("input");
for (var j = 0; j < radio.length; j++) {
if (radio[j].checked)
rbtnValue = radio[j].value;
}
if (rbtnValue == 'Yes') {
args.IsValid = !(args.Value == "")
}
else {
args.IsValid = true;
}
}
</script>
Take care about the property "ValidateEmptyText": If you miss this one, the validation is always "true" for empty fields....
Just to have it complete, here is the RequiredFieldValidator for the radiobutton:
<asp:RequiredFieldValidator ID="MyBtnValidator" ControlToValidate="MyBtn"
runat="server" ErrorMessage="Please make your choice"
SetFocusOnError="true" />
validator
When it comes to deployment of asp.net projects there are several ways, as described for example on MSDN. Working with ASP.net for several years I’ve found the easiest way of deployment for myself is using zip / rar to pack an archive of updated files. This works very well in both company and personal webserver configuration:
- In company I’m not allowed to deploy directly to the live server (as this is an admin job)
- The admin needs to be able to easily deploy the changes to stage server and afterwards to live server.
- The admin needs to be able to identify the changed files easily.
- At home I upload the files to my IIS via FTP as that’s easier than connecting to the machine via RDP and starting an update installation.
- Previously at home with shared hosting I only had access to my webspace via FTP and not via Remote Desktop of course.
Bearing these requests in mind I created a simple batchfile which does this job for me.
Read the rest of this entry
When it comes to deployment of asp.net projects there are several ways, as described for example on MSDN. Working with ASP.net for several years I've found the easiest way of deployment for myself is using zip / rar to pack an archive of updated files. This works very well in both company and personal webserver configuration:
In company I'm not allowed to deploy directly to the live server (as this is an admin job)
The admin needs to be able to easily deploy the changes to stage server and afterwards to live server.
The admin needs to be able to identify the changed files easily.
At home I upload the files to my IIS via FTP as that's easier than connecting to the machine via RDP and starting an update installation.
Previously at home with shared hosting I only had access to my webspace via FTP and not via Remote Desktop of course.
Bearing these requests in mind I created a simple batchfile which does this job for me.
The batchfile is available for WinRar and 7-zip but could be modified easily for every packer which supports command-line packing.
What it does:
It checks the current directory and all subdirectories for modified files.
It takes care of an exclusion list so e.g. your source code (*.vb, *.aspx.vb etc.) will not be added to the packed file.
It adds current date to the filename of the packed file, e.g. 'MyFiles20091231.rar'
If the packed file already exists it will be deleted first.
Configure before first use:
Set Archivename: Line 25 contains a variable 'Archivename'. Set this to a name you like, e.g. 'MyApp%yy%%mm%%dd%.rar'. You might remove the placefolders for current year (%yy%), month (%mm%) and day (%dd%) if you want to have always the same filename, but personally I prefer to have some kind of history, especially as these packed files are normally very small.
Set Path to your packer: Line 31 starts with the filepath of the packer, e.g. '"C:\Program Files\WinRar\Winrar.exe"'. Of course you need to change it if your packer is at another location.
You might take a look at 'Backup-ExcludeFiles.txt'. That's an exclusion list, so all files listed here are NOT added to the packed file. You could use single filenames ('web.config'), wildcards ('*.zip') or names of subdirectories ('TestResults').
Now place all these files into your local application path, e.g. 'C:\inetpub\wwwroot\MyApp'.
That's all for the inital configuration!!
Configuration for every day use:
You've made some changes and want to deploy them? Just edit the 4th line of Backup.cmd. It contains a variable 'LastUpdate', holding the date and time of the last deployment. You have to edit this on your own. Why is it not computed automatically? Because maybe you made a deployment to stage and then add a bugfix. The new packed file of course also needs the files already deployed to stage, because otherwise you will get a problem while deploying to live. So this date should be the latest modification date for all your environments (local test, stage, live).
Now just start the batch file. You see a new file is created. This file could be forwarded to your admin, uploaded to another documentation system, or extracted locally to upload then via FTP to your (shared) hosting!
Download Backup containing Backup.cmd and Backup-ExcludeFiles.txt
asp.net