Wednesday, 24 March 2010

ASP NET group validation

Hi Boy and Girls,

Group validation is a very handy feature of ASP NET. It let you use one validation for a related group of controls,

The scenario:

Let’s say we want to display a list of items that have been updated within a given period range.


text_boxes_control

We want our users to pick a date range, start and end date, and based on the dates picked our application will show the data.  On our UI We have provided one date picker for each text box.

date_picker 

Our application must handle, gracefully,
’abnormal’ scenarios.

Even that our above control has two date pickers, we must handle abnormal scenarios. On this simple example we can see two scenarios that our application must handle:

1. User forgets to input the date on one or both text boxes fields.
2. User doesn’t use the data picker provided by us but instead input the date directly

By looking on these two scenarios we can see that we need two validations for each input box.

ASP NET provide us with “Group Validation”. it is very easy to implement. Basically it is a  3 steps process.


1.  We add the property ‘ValidationGroup’ to each text box. As a value for this property We can use something like “TextBoxesGroup” so we end up with something like this:


<asp:TextBox ID="txtEndDate" ValidationGroup="TextBoxes" runat="server" />

2. We add our ASP NET validator components; Remember we need two of them: one to make our fields ‘required’ AKA compulsory and the other for enforcing the right date format.

So  we will add the <<asp:RequiredFieldValidator/> and the <asp:RegularExpressionValidator>components to our user control.

3. We add the property ValidationGroup to each of our validatos.  As a value for the ValidationGroup property we must specify the value that we used for the text boxes property ValidationGroup.

<asp:RequiredFieldValidator ID="RequiredValidator” runat="server" ValidationGroup="TextBoxes"
      ControlToValidate="txtStartDate"  ErrorMessage="your error message”" />

<asp:RegularExpressionValidator  ID="DateRegularExpressionValidator" ValidationGroup="TextBoxes"
        ValidationExpression="your RegEx expressions"
ControlToValidate="txtStartDate"   ErrorMessage="You error message" runat="server" />

4. We add the property ValidationGroup="TextBoxes" to the submit button.We will need this so our SearchProductsButton only validate the controls grouped under the TextBoxes validation group.

<asp:Button ID="SearchProductsButton" runat="server" OnClick="Search_Click" Text="Search Products" ValidationGroup="TextBoxes" />


Other solutions:

1.We could have built our validation on the server code, otherwise known as ‘code behind. This solution will cause the validation to be run on the server side, generating extra round trips.

2. Make our text boxes read-only. If we make our text boxes read only we will still able to populate them using JavaScript, however, If you try to retrieve the text box values, from the code that is tied to the user control or page, you will see only emptiness. Well not emptiness; You will see an empty string for each text box control value.

I tested my solution using IE 8 and Chrome and it worked on both of them.

If some one was able to retrieve the values of  read-only text boxes, from the code behind, let me know. :

-)

   Don’t underestimate the power of the dark side.

No comments:

Post a Comment