Wednesday, March 4, 2009

ASP.NET Validator in Modal Popup Extender

Yesterday,
I had an interesting issue, which I want to share with you.

The Case
I had a RequiredFieldValidator in a Modal Popup Extender. After closing the popup, everything in my web page freezed. For example, clicking a LinkButton/Button wouldn't do a postback, everything just stopped working.

The Problem
After some kind of debugging, I figured out the problem. As you know, the modal popup extender works like this:
1.) When you invoke the .Show() method of the Modal Popup Extender, the AJAX.NET takes the PopupControl you want to Show as a popup and brings it to the front.
2.) When you invoke the .Hide() method or close the Modal Popup Extender from a button inside it, it again takes the PopupControl and hides it in the page.
So, the problem I encountered was, that when the user entered some invalid data and presses the cancel button(to close the ModalPopupExtender and to cancel the changes), the AJAX.NET hided the PopupControl in the page. The problem was, that the RequiredFieldValidator was yielding that there is invalid data, so it was blocking all the postbacks. The cute thing is, that you can't actually see the RequiredFieldValidator, because it's in the Modal Popup which you don't see, too.

The Solution
The simplest thing to do, was to clear all the Validators from the ModalPopupExtender, when I hide it. This is not a neat solution for me, but I don't know any better for now.

Conclusion
Probably, people encounter such a 'problem' on rare occasions, but I found it useful for myself, and will be aware of this.

8 comments:

Veselin Kolev said...

Hi, Yosif!

Very interesting situation. Actually the solution which I suggest is to put the controls in the popup extender to be in their own validation group. I think that this should fix your problem.

Thanks for your unstoppable contribution to the community!

cypressx said...

Hi, Vesko !

The solution you suggested is more than correct. I tried it and it worked.

Thanks for your support ! :)

platec said...

The validation group solution worked like a champ.

I'm still pretty new to .NET and struggled with this problem for a number of hours today. Can you explain in a bit more detail how you went about debugging it to uncover the issue?

cypressx said...

Hi platec,
in this case I actually figured out the problem, because I researched how does the ModalPopupExtender worked. With it's mechanism in my mind - (when you show the ModalPopupExtender it brings you the panel to the front, and when you hide it, it returns back the panel into the page) - while I was explaining and demonstrating a colleague this issue, I figured out why the button didn't trigger.


I can say, that it's often that I find out the solution of a problem, just when I am explaining it to some other person. Probably, when explaining it to someone else I think more deeply about it.

Anonymous said...

Very useful solution. Thanks a bunch for posting such good solution. I was really stuck with this problem for 2 days.

Anonymous said...

The solution below worked for me:

Very interesting situation. Actually the solution which I suggest is to put the controls in the popup extender to be in their own validation group. I think that this should fix your problem.

Anonymous said...

You can try do this with javascript:

Page_ValidationActive = false;

This must say you page that there is no validations actives, and must work as fine or as worst as in the begining.

Anonymous said...

Thanks for this post, It helps me a lot. I had been struggled for several days trying to solve this problem.
Thanks again.