dotnet web sql business etc

22 квітня, 2011

Implementation of a simple algorithm to generate a random alpha-numeric string of a needed size

Below is an implementation of a simple algorithm to obtain an alpha-numeric id, I found on the web, neat and easy:

public static string GenerateUniqueCode(int maxSize)
        {
            var allowedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var random = new Random();
            var result = new string(
                Enumerable.Repeat(allowedChars, maxSize)
                          .Select(s => s[random.Next(s.Length)])
                          .ToArray());
            return result;
        }

Мітки: , ,

Validating several controls with one customvalidator: using ValidatorHookupControl javascript function

Recently I have run into a seemingly simple problem: I had to make a control for picking a birth date.

First, I thought that using a MaskEdit control from AjaxControls toolkit would be the best fast and simple solution. However, then it turned out that MaskEdit is quite limited, in terms of validating date type input.

Afterwards, my colleagues recommended using dropdowns for days, months and years. That is easy, first we populate the dropdowns with the needed options (days 1-31, months Jan - Dec, years 1900-2011). Yet, there are some invalid combinations of values that can be entered by the user. Thus, input has to be validated, when either day, month, or year value changes, all three dropdowns have to be validated as a whole. This problem lead me to find out a solution on how to link several input controls to one custom validator by using ValidatorHookupControl.

First, we should define the following javascript function for our file:

function HookupControl(curObj, customValidatorClientID) {
         var customValidator = document.getElementById(customValidatorClientID);
         customValidator.controltovalidate = curObj.id;
         customValidator.clientvalidationfunction = 
"ValidateDate";

         ValidatorHookupControl(curObj, customValidator); }

this function links the input control (curObj) to your custom validator control (customValidator) at runtime.

Afterward, we should have HookupControl function run everytime user changes the value of either field (day, month, year). To do that, we have the define the following code for each of the dropdowns in the Page_Load event of the control (in addition to populating control with possible values):

ddlDay.Attributes.Add("onfocus", "HookUpControl(this,'" + customValidator.ClientID + "')");


Nice solution. Works as a charm!

Мітки: , ,

19 квітня, 2011

Rhino Mocks: how to clear/reset expectations when using same test method several times

Today I faced a seemingly simple problem of running one unit test method with different parameters stored in arrays. When looping through the arrays of data, I was setting expectations for different set of arguments. Well, in the third loop, I have set the different expection results for the set of arguments equvalent to the one in loop number two. Althrough I have "overriden" the expectation, the test method was still using the first expectation set. The solution was to add Repeat.Once() call to the end of the expectation set line to make sure it is used only once.

Мітки: ,

07 квітня, 2011

Client validations with custom validators in .NET: why it might not work

I have just spent about an hour trying to find out why client validation with clientvalidationfunction did not work. Well, unfortunately for myself I have used args.isValid parameter instead of args.IsValid (capital I) in my javascript function. Daaaah... :)

Мітки: , ,