Tuesday, March 10, 2009

Validate A Control When Its Focus Is Lost


Hi,

I tried to do this when one of my friend asked me for this. This is actually a good idea, instead of validating the controls after submitting the page. So I searched over the net to achieve this and finally found a piece of code. Thought to put this here so that it might be helpful to others as well :)

I took 2 TextBox on the page with 2 RequiredFieldValidator, one for each TextBox. Then go to the Page_Load event of the page and give the following code, which will add the "onblur" event to each TextBox with the "ValidatorOnChange" event which is actually raised when a control needs to be validated, this is already declared in the validation scripts.


TextBox1.Attributes.Add("onblur","ValidatorOnChange(event);")
TextBox2.Attributes.Add("onblur","ValidatorOnChange(event);")



That's it... this will do the work. In case if you want to restrict moving to next control until the control is properly validated, give SetFocusOnError property of the Validation Control to "true".

Hope this will help you...

Monday, March 2, 2009

Combine Array values to String



Hi, in this section I wanted to tell you a simple way to combine an Array of values in
to string delimited by a character. We can do this very easily by using "String.Join". Below is a simple example as how we can do this.


Dim aTemp As String() = {"1", "14", "23", "7", "13", "37", "143"}
Dim sDelimiter As String = ","
MsgBox(String.Join(sDelimiter, aTemp))


We need not loop through and combine with some logic which will even increase the processing time, this function will do it as we wanted. We can specify what ever delimiter we want.

Hope this will help you at some point of time. Happy Coding :)