Invoke client side events from a custom method
I have a project I am currently working on that has multiple validators and events when a postback is created (classic asp validator controls).
I have a client method called when the submit button is clicked. It makes ajax calls to various services and once everything is done a postback is done (via a classic document.forms[0].submit()
for now).
The problem here is that my javascript method is run before every other javascript calls on the event queue. The second problem is that my method returns false to block the PostBack (I do async calls so I need to make sure a PostBack is made only when I am sure every processing is completed).
I would like to either call this method at the end of the validation queue or manually call each validations inside of the method itself and then run the ajax calls.
Here is a POC to explain the problem (plain simple page):
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">开发者_如何学Go
<script type="text/javascript">
Validate = function () {
setTimeout(function () { document.forms[0].submit() }, 2000);
return false;
}
</script>
<asp:TextBox runat="server" ID="txtSomething"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" onclientclick="return Validate();" Text="Button" />
<asp:RequiredFieldValidator runat="server" ID="valRequiredTruc" ControlToValidate="txtSomething">something is required</asp:RequiredFieldValidator>
</asp:Content>
Here, the Validate function blocks the postback to do something anything. After 2 seconds a Postback is created and the RequiredFieldValidator validate contents but server side. I would like to make it validate client side.
maybe the javascript function Page_ClientValidate() can help you. using this method you can trigger the page validations.
http://dotnetslackers.com/Community/blogs/kaushalparik/archive/2011/02/28/executing-server-validators-first-before-onclientclick-javascript-confirm-alert.aspx
精彩评论