Mar
27
2007

ASP.NET AJAX and ASP.NET Validators

If you are using ASP.NET Validators in UpdatePanel consider the post of Matt Gibbs : "ASP.NET AJAX Validators".

ASP.NET AJAX provides new APIs for registering script with the ScriptManager.  Using these APIs allows controls to work well with partial rendering.  Without them, controls placed inside an UpdatePanel won't work as expected. In previous CTP releases of ASP.NET AJAX, we had a set of validator controls that derived from the v2.0 controls and used the new APIs. This made them work well with ASP.NET AJAX. WindowsUpdate will soon include a version of System.Web that can take advantage of the new APIs.  So the new controls which would have been redundant have been removed.  However, the update isn't available yet and ASP.NET AJAX has been released.  So, in the short-term, the source code for a set of custom validator controls that work with partial rendering is available here.

The .zip file includes a solution and .csproj file as well as the compiled DLL.  Just put the DLL in the /bin directory of your application and include the following <tagMapping section in the pages section of the web.config.

      <tagMapping>
        <add tagType="System.Web.UI.WebControls.CompareValidator"           mappedTagType="Sample.Web.UI.Compatibility.CompareValidator, Validators, Version=1.0.0.0"/>
        <add tagType="System.Web.UI.WebControls.CustomValidator"            mappedTagType="Sample.Web.UI.Compatibility.CustomValidator, Validators, Version=1.0.0.0"/>
        <add tagType="System.Web.UI.WebControls.RangeValidator"             mappedTagType="Sample.Web.UI.Compatibility.RangeValidator, Validators, Version=1.0.0.0"/>
        <add tagType="System.Web.UI.WebControls.RegularExpressionValidator" mappedTagType="Sample.Web.UI.Compatibility.RegularExpressionValidator, Validators, Version=1.0.0.0"/>
        <add tagType="System.Web.UI.WebControls.RequiredFieldValidator"     mappedTagType="Sample.Web.UI.Compatibility.RequiredFieldValidator, Validators, Version=1.0.0.0"/>
        <add tagType="System.Web.UI.WebControls.ValidationSummary"          mappedTagType="Sample.Web.UI.Compatibility.ValidationSummary, Validators, Version=1.0.0.0"/>
      </tagMapping>

Comments (28) -

http://

The RTW doesn't require this anymore. This info is outdated.

http://

Ohh nevermind Smile Now I see the additional download! Do'h

mhawley: It's absolutely NEEDED!!!! with RTW.

http://

"WindowsUpdate will soon include a version of System.Web that can take advantage of the new APIs."

Two months and still no update? I don't like their definition of "soon"!

http://

I am using dotnetnuke.  I have placed the validators.dll in my bin, and added the tagging to my web.config.  The validator now works but only when I'm logged in to my app as a registered user.  Is there any special authentication or permission required for these compatible validators to work?

Robby: it is working ok here without any fancy settings.

This only works for declaratively added validators!  The Tag mapping is used only for the ASPX code generation/compile phase and DOES NOT help for validators you add programmatically.  What we need until the "shortly" passes is a way to sniff that section of the configuration to do the remap when injecting validators.

A better stop-gap would probably be to add an Adapter that does the right thing...

Marc: Right I used it only declaratively!

http://

I am also suffering from the same problem, I have controls which are programmatically created and since adding the .dll to the bin directory and the tagMapping they are just completely ignored, two of my other pages in the same app which have the controls which were dragged and dropped to the page work fine.

What was interesting was prior to doing the above, half of my original validators worked!  I have 2 drop down lists and a validator next to each one, the first validator worked fine and as it should, the second one fired, ie, the submit button wouldn't take you on, but the image (which I'd added as "Text" for the validator) just didn't appear on the page.

Well odd...not sure what to do now - it seemed for me I was closer prior to using the above, as I've been working on this since October and see no obvious end to my project perhaps I should revert back and just hold out for the official fix to the problem?

http://

Can't you use dynamically thus?:

Dim requiredValidator As New Sample.Web.UI.Compatibility.RequiredFieldValidator

http://

I downloaded the zip files and added the dll in my project bin directory and added the tagMapping in web.config. I am using formview inside an update panel. But my problem is not resolved still. I have downloaded the update patch recommended Mr. Matt Gibs site from microsoft. I think validator.dll is not compatible after the path is installed. Can anyone help in this regard.

http://

Same problem here. I am using a MultiView control inside the UpdatePanel. Did as instructed - placed the DLL in \bin and added the configuration to Web.Config - but it's not working.

http://

Related to my previous comment: I have found out the reason why the Validation controls were not working in the UpdatePanel. It is because of the ValidationGroup property. I had had grouped my validators for the UpdatePanel by setting their ValidationGroup property to prevent them from validating their connected controls with the other validators on the page. However, it is surprising to see that there is no need to set the ValidationGroup property for controls if being placed in an UpdatePanel. Removing the ValidationGroup property solves the problem and the validators work fine.

Haroon MAlik: Thansk for the feedback it might help others.

http://

Haroon Malik, thank you for your update and help. My scenario is bit different. I have two Form controls in two different update panels (either the update pannels are nested or separate). It is a 1 to many relationship. So I had to separate the validators by using the ValidationGroup. But I think this is the bug in the API. As there will be scenarios where you would like to separate your validators. Anyway I have to come up with some different logic to overcome the problem. Again Thanks.

http://

Haroon Malik,

Thanks, problem solved now.

But why setting ValidationGroup causes these problems?
There is a way to make validators works setting up a ValidationGroup?

Matt Gibbs Solution doesn't work for me. But removing ValidationGroup does...

http://

RegularExpressionValidator ErrorMessage is not displayed.... just blink for once...

http://

I've got aproblem with validators using the
I'm doing a composite control in C# with a requiredFieldValidator and it works with AJAX, and when the page goes to the server, the errorMessage and te Text of the validator doesn't show, only appear in a SummaryValidator, I lose the messageError and the text of the validator.

Thanks.

http://

Thanks a million. Solved my problem on the server.

http://

This worked for me. The validators work when I submit the page, or if I enter a bad value after the updatePanel has posted back. However I need the validators to keep their states (show their error messages or not) after UpdatePanel Post Back. That is:
I have a DropDownList on my UpdatePanel that does a postback. All the already validated controls values/states disappear. The validators still work, when I re-trigger them so to speak.

I am using a Repeater control inside the UpdatePanel. Did as instructed - placed the DLL in \bin and added the configuration to Web.Config - but it's not working.

Pls help

http://

Hey, thanks for the info.  Just spent about 4 hours trying to figure this out. this solution seems to work perfectly

Thank you friend.

It is working fine.

lalitha

http://

I copied the dll to bin folder and do the same to webconfig, other validator are working fine except validationsummary, after clicking submit button, validationsummary shows multiple times of errormessage. It looks like whenever i do a partial postback with an updatepanel, the summary shows once more. For example, if partial postback twice, then the error message will be displayed twice. It is really weird, any ideas?

http://

Thanks Marc Brooks. I was really struggling with this problem. Now its solved.

Thanks, It was giving me great headache. Thank you again for the solution!

http://

I've included the dll and updated the web.config.  I'm still stuck with the same issue i.e.

Validators in a ModalPopupExtender control.

Validators work fine.  As soon as i press OK with valid info the form doesn't post.

Any ideas guys?

http://

thanks...
work well...

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About Laurent

Laurent Kempé

Laurent Kempé is the editor, founder, and primary contributor of Tech Head Brothers, a French portal about Microsoft .NET technologies.

He is currently employed by Innoveo Solutions since 10/2007 as a Senior Solution Architect and certified Scrum Master.

Founder, owner and Managing Partner of Jobping, which provides a unique and efficient platform for connecting Microsoft skilled job seekers with employers using Microsoft technologies.

Laurent is awarded by Microsoft since Avril 2002: Most Valuable Professional (MVP).

MVP
Certified ScrumMaster
JetBrains Academy Member

My status

Twitter

Flickr

www.flickr.com
This is a Flickr badge showing public photos and videos from Laurent Kempé. Make your own badge here.

Month List

Page List