从自定义方法调用客户端事件

发布于 2024-12-02 16:25:32 字数 1174 浏览 0 评论 0原文

我目前正在开发一个项目,该项目在创建回发时具有多个验证器和事件(经典的 asp 验证器控件)。

单击提交按钮时会调用一个客户端方法。它对各种服务进行 ajax 调用,一旦一切完成,就会完成回发(目前通过经典的 document.forms[0].submit() )。

这里的问题是我的 javascript 方法在事件队列上的所有其他 javascript 调用之前运行。第二个问题是我的方法返回 false 以阻止回发(我执行异步调用,因此我需要确保仅当我确定每个处理都已完成时才进行回发)。

我想在验证队列的末尾调用此方法,或者手动调用方法本身内部的每个验证,然后运行 ​​ajax 调用。

这是一个解释问题的 POC(简单的页面):

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <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>

这里,Validate 函数会阻止回发执行任何操作。 2 秒后,将创建回发,并且RequiredFieldValidator 验证服务器端的内容。我想让它验证客户端。

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">
    <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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

前事休说 2024-12-09 16:25:32

也许javascript函数Page_ClientValidate()可以帮助你。使用此方法您可以触发页面验证。

http://dotnetslackers.com/Community/blogs/kaushalparik/archive/2011/02/28/executing-server-validators-first-before-onclientclick-javascript-confirm-alert.aspx

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文