开发者

How to excecute Javascript after failed form validation

开发者 https://www.devze.com 2023-03-30 06:20 出处:网络
I am using ASP.NET 3.5, and the jquery.hint.js plugin. It works great, with one small problem. I have various ASP.NET validators on the page and when the page fails to validate, client-side, all of th

I am using ASP.NET 3.5, and the jquery.hint.js plugin. It works great, with one small problem. I have various ASP.NET validators on the page and when the page fails to validate, client-side, all of the textboxes that contained hint text are now blank. This is confusing for the end-user because their are no field labels. To see the hint text, they must click in and out of the field and it re-populates.

Not sure if it makes a difference, but these fields are wrapped in an ASP.NET AJAX UpdatePanel. The reason for the update panel is to do some asynchronous calls for dynamic field updates like auto-populating the state dropdown based on what country you choose. The form is submitted via postback.

I need a way to call the hint javascript against these fields after the validation fails on the client side. How can I do this?

Here is an example of the client side code that executes the hint plugin against the various fields on the page:

<script type="text/javascript">
    $(document).ready(function () {
        // Add handler function to UpdatePanel's events
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
        PageLoad();
    });

    function PageLoad() {
        RetainState();
    }

    // This function handles the UpdatePanels' EndRequest event.
  开发者_运维技巧  function EndRequestHandler(sender, args) {
        if (args.get_error() == undefined) {
            RetainState();
        }
        else { // there was an error
            alert("There was an error" + args.get_error().message);
        }
    }

    function RetainState() {
        // Add hint to textboxes.
        $(":text").hint();
        $("textarea").hint();
    }
</script>

One last thing: I thought I had already fixed this problem by adding the following code to the Page_Load method in the code behind, but if it was working before, it is not now:

// This will call some javascript after a form validation fails
// and keep the hint text in the form fields.
if (!Page.ClientScript.IsOnSubmitStatementRegistered("KeepState"))
{
    string script = "RetainState()";
    Page.ClientScript.RegisterOnSubmitStatement(Page.GetType(), "KeepState", script);
}

Thanks,

Tod

0

精彩评论

暂无评论...
验证码 换一张
取 消