Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Lazy Developer

by 'Jimmy' Skowronski


Atlas Extenders Crash Course - Part 3 – The Page Integration

Welcome again in the last part of this series. So far we made the extender with bunch of features and then we had migrated to the new ASP.NET AJAX framework. So there only one left to do in order to get fully functional extender. As a current state our course contains the following parts:

  • Introduction – what Atlas Extender is and how to create our own validation extender,
  • Page methods – how to add AJAX server-side validation,
  • Migration – from Atlas to ASP.NET 2.0 AJAX,
  • Integration with the page – how to integrate extender with the ASP.NET page to perform full form validation.

In order to make the extender usable as a part of the page, we have to put it into validation cycle of the page. What we have to do is to find a way, how to stop form before submitting till all server-side validation requests are not accomplished. Let us first look at normal validation cycle and how our extender fit into that cycle.



Simplified submit cycle is shown on figure 1. Basically, function WebForm_OnSubmit() is attached the form's onsubmit event. This function is very simple itself and returns true, which allows submit, if validation is successful. Using validation groups makes all things a bit more complicated, but everything is done in scripts that are located in resources, so we will not care about them. In the normal page when all validators perform their validation successful, page is submitted, otherwise error messages are displayed, but when we insert our extender all cycle will be broken (Figure 2).


When the extender is set up to perform the server-side validation, the validation method returns false which stops the page from being submitted. That is quite reasonable, as we have to wait for the server-side validation results. The main problem is that we can't perform submit just after the server-side validation ends successfully because there can be more than one extenders and all the server-side validation checks pending and needs to be completed before we will decide if we can allow submit or not. So the best way is to count somehow how many server-side validations started and check this counter after each one is finished (Figure 3).

In fact, there are only two changes in the code we have to do. First what we will need is to count all server-side validations. In order to do this, we will add two global variables to store the counters. Then, we have to modify slightly the extender behaviour script and add a few lines into validate() and _onMethodComplete() functions (I've removed rest of function bodies in order to make changes more visible):

 

So it's quite simple. You can see two counter variables there __AtlasValidatorExtendersInvalid and __AtlasValidatorExtendersInProgress. First one is the number of the invalid results while second one is the number of validations pending. Other world, when __AtlasValidatorExtendersInProgress equals zero then all validation calls were completed, and when __AtlasValidatorExtendersInvalid equals zero then all validation were successful. So, before we will call the server-side validation method, both counters are increased and later then, when asynchronous call is completed, counters are decreased according to the result. At the end of _onMethodComplete() you can notice I'm calling formSubmitMockup() function.

This function is a mock-up function that is used to intercept the form submit event and forces it to wait for validation results. In simple format, that function looks as follow:

 

As you can see, there is no magic here. First we are calling an original submit function WebForm_OnSubmit() and its result is remembered for later use. Because this original function will cause validation, so all asynchronous validations will be started and our counters will be set at some value. Then counter is checked and if is greater than zero, false is returned to stop form being submitted. After each of the server-side validations is completed counters are decreased and this mock-up function is called and every time counters are checked and if both are equal zero, the form is submitted.

Actually this mock-up function and counters has to be added to the page only once, so can't be included in behaviour script. Instead if that, we will add OnPreRender() event to the extender class. Finally then, all additional code looks as follow:

 

Well, looks as we finished that small course, hope you enjoyed it. I hope I explained what I've done and all of this is readable and understandable as well. You can download all sources and binary files from the project page I've set up yesterday on the codeplex. As my intention is to create a bit more than one validator, you can expect that this page will grow soon with new extenders. If you will have any questions or comments use blog or codeplex page as prefer.



Comments

Jimmy said:

Right. Forgot to mention, sorry. Yes, they solved this issue and now all code behind web methods are working fine.

# November 22, 2006 3:44 AM

Sonu Kapoor said:

Great post!

# November 23, 2006 10:18 PM

About Jimmy

I currently work for MessageLabs, one of the best security companies in the world! I love the Microsoft technology stack and use it to make my company successful! Not only work is my life. I love mountains and climbing. That feeling when I'm on the wall and there is nothing around me, only void. When I can feel stone on my fingers, every tiny bit, when my fingers are going wet and chalk doesn't help, when my muscles and body are exhausted. Damn, I'd love it. Even more than programming. Check out Devlicio.us!

Our Sponsors

Red-Gate!