Tuesday, July 29, 2008

Force Page Client Validation First....

Today, I need to force check if the Page is valid in the client to perform another javascript function called.

Luckily, If you use Script Manager, WebResources.axd has been provided a function.

This function called Page_ClientValidate which will return the boolean result which indicate if the page client is valid / not. This function also will so the error message from page client validator and summary validator.

validationResult = Page_ClientValidate("groupName");

However if you use validation which is happen on the server, you still to mark your button CausesValidation="true" (Default) and code behind to check if Page is valid.

Here is example server validator using Custom validator

<asp:CustomValidator ID="custVal" runat="server" Display="dynamic" OnServerValidate="custVal_ServerValidate">
*
</asp:CustomValidator>
//code behind validation
protected void sitevalName_ServerValidate(object source, ServerValidateEventArgs args)
{
if (not valid)
args.IsValid = false;
}
//and don't forget in your button click code behind
//you need to check if page is valid,
//otherwise it will keep going.
if (!Page.IsValid) Return;

No comments: