views:

216

answers:

2

I have a composite User control for entering dates:

alt text

The CustomValidator will include server sided validation code. I would like the error message to be cleared via client sided script if the user alters teh date value in any way. To do this, I included the following code to hook up the two drop downs and the year text box to the validation control:

<script type="text/javascript">
    ValidatorHookupControlID("<%= ddlMonth.ClientID%>", document.getElementById("<%= CustomValidator1.ClientID%>"));
    ValidatorHookupControlID("<%= ddlDate.ClientID%>", document.getElementById("<%= CustomValidator1.ClientID%>"));
    ValidatorHookupControlID("<%= txtYear.ClientID%>", document.getElementById("<%= CustomValidator1.ClientID%>"));
</script>

However, I would also like the Validation error to be cleared when the user clicks the clear button. When the user clicks the Clear button, the other 3 controls are reset. To avoid a Post back, the Clear button is a regular HTML button with an OnClick event that resets the 3 controls. Unfortunately, the ValidatorHookupControlID method does not seem to work on HTML controls, so I thought to change the HTML Button to an ASP button and to Hookup to that control instead. However, I cannot seem to eliminate the Postback functionality associated by default with the ASP button control. I tried to set the UseSubmitBehavior to False, but it still submits. I tried to return false in my btnClear_OnClick client code, but the code sent to the browser included a DoPostback call after my call.

btnClear.Attributes.Add("onClick", "btnClear_OnClick();")

Instead of adding OnClick code, I tried overwriting it, but the DoPostBack code was still included in the final code that was sent to the browser.

What do I have to do to get the Clear button to clear the CustomValidator error when clicked and avoid a postback?

   btnClear.Attributes.Item("onClick") = "btnClear_OnClick();"
+1  A: 

Try adding a return false; at the end of your onClick call. That should prevent the default behavior (in this case submit).

btnClear.Attributes.Add("onClick", "btnClear_OnClick(); return false;")
R0MANARMY
Very good. That answered my question. Unfortunately, ValidatorHookupControlID doesn't seem to work on a Button control, so I had the usercontrol emmit this script: strScript1 "
Velika
@George: Are you sure it doesn't work on regular html elements? Looks like the function just needs the correct IDs.
R0MANARMY
I don't think the ValidatorHookupControlID method works when passing an HTML or an ASP button.
Velika
A: 

Are you wanting to clear the error message on the client side if they fix the error without having to click on anything right?

I did something like this if I have your issue right but calling a revalidation function that called some client side javascript code and then hides the span that the error message is in if they fixed the issue.

See my blog article and let me know if this is what you are wanting to solve. The part you want to read is towards the bottom.

http://coding.infoconex.com/post/ASPNET-CustomValidator-that-validates-multiple-controls-using-both-Server-Side-and-Client-Side-scripting.aspx

Jim Scott