Why does a failed validation prevent postbacks from control in other validation groups

asp.netpostbackvalidationwebforms

I have an asp.net web form with several textboxes, buttons, and other controls. One of the buttons is supposed to trigger validation of content in some of the textboxes. This works fine, except that if validation fails, all subsequent postbacks of the page are prevented. Fixing the invalid values gets rid of the error messages, but postback still does not occur.

The validation is occurring within an AJAX UpdatePanel. I'm not sure if that matters or not.

Best Answer

I'm glad the workaround is fine for you, but for the benefit of others who land this page, disabling client side validation is far from an acceptable solution. So here goes:

Why it happened only when UpdatePanel was around, is still unclear to me, however:

As others have mentioned, the issue you were having is most likely because the unsuccessful validation attempt is preventing a further postback.

See also: Why won't my form post back after validation?

There is a variable called Page_BlockSubmit which is set to true when client side validation fails:

function Page_ClientValidate(validationGroup) {
    ....
    Page_BlockSubmit = !Page_IsValid;
    return Page_IsValid;
}

So when a __doPostBack occurs as part of, say, an OnSelectedIndexChanged of a DropDownList, there is this check:

function ValidatorCommonOnSubmit() {
    ....
    var result = !Page_BlockSubmit; /* read the value */
    ....
    Page_BlockSubmit = false; /* reset it */
    return result; 
}

Which will block the first time, but then clear the flag, so the next postback attempt should work.

The workaround, in my case, for a DropDownList's OnSelectedIndexChanged event, was to have a snippet added to its client-side onchange event:

<asp:DropDownList runat="server" ID="SomeDropDownList" OnSelectedIndexChanged="AlwaysDoSomething"
   onchange="resetValidationState(this);">
    ....
</asp:DropDownList>
<script type="text/javascript">
    function resetValidationState() {
        // clear any postback blocks, which occur after validation fails:
        window.Page_BlockSubmit = false;
    }
</script>

You might get away with putting that snippet into the onchange itself.

Related Topic