Tuesday, May 17, 2011

[ASP.NET] RequiredFieldValidator + OnClientClick issue

source : http://alvinzc.blogspot.com/2006/10/aspnet-requiredfieldvalidator.html



OnClientClick property was introduced in ASP.NET 2.0 to eliminate the need of developers to add the client-side "onclick" to a control programmatically. For instance,

JS script

function ConfirmMe()
{
   return confirm("Do you want to proceed?");
}


ASPX





Well, that is pretty straightforward. BUT, it goes weird when you have a validator control (eg. RequiredFieldValidator) that is used to validate the "txtName" textbox server control. For instance,









Whenever you press the button with no textbox value, the client-side confirmation dialog will be invoked first before the validator message is able to show up. This isn't what we expected it to behave. I tried several ways to overcome this problem, including using CLIENT CALLBACK, disabling the CauseValidation, but it failed. Finally, I was able to find a solution by adding JUST ONE line in the JS script.

function ConfirmMe()
{
   if(Page_ClientValidate())
      return confirm('Do you want to proceed?');

   return false;
}

No comments:

Post a Comment