Click here to Skip to main content
Email Password   helpLost your password?

Introduction

While validation groups are part of the new Visual Studio 2005, for the previous versions of Visual Studio .NET, group validation was not an easy job.

This article will give you custom ASP buttons and image buttons that have an additional property field called "attachedValidators" that takes a comma separated list for the validator names that will be activated when this button is clicked.

The Code

The idea is to run a JavaScript before provoking validators on the page that will disable all validators and only enable the validators specified in the "attachedValidators" property.

The JavaScript code is as follows:

for(i=0; i<Page_Validators.length; i++)
{
    ValidatorEnable(Page_Validators[i], false);
}

This for loop disables all validators on the page. Page_Validators, ValidatorEnable are both defined for any page that contains ASP.NET validators.

ValidatorEnable(rqVal1, true);
ValidatorEnable(reVal1, true);

After that, I enable the list of validators that are defined in the property of each button.

if (typeof(Page_ClientValidate) == 'function')
    Page_ClientValidate();

Finally, I add the code that ASP.NET by default adds for any button if the button's CausesValidation property is set to True.

So, this JavaScript code is added to each instantiated button by overriding the AddAttributesToRender function, the function looks as follows:

Protected Overrides Sub AddAttributesToRender( _
        ByVal writer As System.Web.UI.HtmlTextWriter)
    If _ValidatorsIDs.Length > 0 Then
      Dim scrptValidation As New System.Text.StringBuilder
      scrptValidation.Append("javascript:for(i=0;" & _ 
          " i<Page_Validators.length; i++)" & _ 
          " { ValidatorEnable(Page_Validators[i], false); } ")
      Dim enabledValidators As String() = _ValidatorsIDs.Split(",")
      For i As Integer = 0 To enabledValidators.Length - 1 Step 1
        scrptValidation.Append("ValidatorEnable(" & _
                enabledValidators(i).Trim & ", true); ")
      Next
      scrptValidation.Append("if (typeof(Page_ClientValidate)" & _ 
                 " == 'function') Page_ClientValidate(); ")
      writer.AddAttribute("onclick", scrptValidation.ToString()) 
    End If
    MyBase.AddAttributesToRender(writer)
End Sub

Conclusion

Finally, you can use the custom buttons by building the ValidationButtons project, then right clicking on your ToolBox menu, then from the Add Item dialog, choose validationButtons.dll, and the ButtonGrpValidation and ImageButtonGrpValidation components should be added to the list of your Web Forms components.

It is really simple and you can also check the demo attached with this article to see it in action.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralPage Validation
taersious
8:59 27 Feb '07  
Thank you for pointing me in the right direction. Poke tongue

I needed to set a field to validate only when a radio button control was set a a specific option. This was complicated by the fact that I am using DotNetNuke, which renames all of my controls.

Here is my Javascript that is used, along with an onlclick event on the radiobuttonlist. I just reference the RequiredFieldValidator by the ID tag I use and the 2nd function finds that rfv for me, and sets its Page_Validators property accordingly.

function clientSetRequired(a)
{
var c;
var sRet='';
c=FindCheckedDNNClientConrol(a);
sRet = c.value;
switch (a)
{
case 'radPhoneOption':
if(c.value=='None') ValidatorEnable(Page_Validators[FindValidatorControl('rfvPhone')], false);
else ValidatorEnable(Page_Validators[FindValidatorControl('rfvPhone')], true);
break;
case 'radFaxOption':
if(c.value=='None') ValidatorEnable(Page_Validators[FindValidatorControl('rfvFax')], false);
else ValidatorEnable(Page_Validators[FindValidatorControl('rfvFax')], true);
break;
}
}
function FindValidatorControl(NamePortion)
{
var iRet;
for(i=0; i {
if(Page_Validators[i].id.indexOf(NamePortion)>0) iRet = i;
}
return iRet;
}

I hope this saves someone some time, as it would have helped me.


>drew< Poke tongue


Last Updated 24 Nov 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010