5,550,131 members and growing! (19,581 online)
Email Password   helpLost your password?
Web Development » Validation » General     Intermediate

Group Validation Buttons

By Tamer Safwat Aziz

This article will teach you how to create your own ASP.NET buttons and image buttons that enable only a set of validators that are attached to these controls on your ASP.NET page.
VB, Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 15 Nov 2005
Updated: 24 Nov 2005
Views: 15,680
Bookmarked: 11 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.41 Rating: 3.10 out of 5
2 votes, 33.3%
1
0 votes, 0.0%
2
1 vote, 16.7%
3
2 votes, 33.3%
4
1 vote, 16.7%
5

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Tamer Safwat Aziz


B.Sc. Computer Science.
Independent Developer.
Interested in C/C++/C#/Win32API/Bluray Java development

Author of MagicMerge and Phonashera applications.
Occupation: Software Developer
Location: Egypt Egypt

Other popular Validation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralPage Validationmembertaersious8:59 27 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Nov 2005
Editor: Smitha Vijayan
Copyright 2005 by Tamer Safwat Aziz
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project