如何在客户端取消服务器控件事件?

时间:2021-12-28 15:37:17

I have an asp.net image button and I want to cancel the click event incase he fails the client side validation... how do I do that?

我有一个asp.net图像按钮,我想取消点击事件,因为他没有通过客户端验证...我该怎么做?

3 个解决方案

#1


1  

<asp:ImageButton OnClientClick="return ValidatorOnSubmit()" />

The ValidatorOnSubmit() function should already be included by the ASP.NET framework if you have validators on your form. The standard WebForm onsubmit function looks like this:

如果表单上有验证器,则ASP.NET框架应该已包含ValidatorOnSubmit()函数。标准WebForm onsubmit函数如下所示:

function WebForm_OnSubmit() {
   if (   typeof(ValidatorOnSubmit) == "function" 
       && ValidatorOnSubmit() == false) return false;

   return true;
}

#2


0  

There is an OnClientClick event you can set this to your javascript function. If you return true it will continue to the post back. If you return false the post back will not happen.

有一个OnClientClick事件,您可以将其设置为您的JavaScript函数。如果您返回true,它将继续回发。如果您返回false,则不会发回帖。

<asp:Button ID="NavigateAway" runat="server" OnClientClick="javascript:return PromptToNavigateOff();" OnClick="NavigateAwayButton_Click" Text="Go Away" />

  <script type="text/javascript">
    function PromptToNavigateOff()
    {
        return confirm("Are you sure you want to continue  and loose all your changes?");
    }

  </script>

#3


0  

I would simply reverse logic and not allow the user to click the button until he has filled the information. Put mandatory markers and if it is filled it then the button is enabled.

我只是简单地反转逻辑,不允许用户点击按钮,直到他填写了信息。放置强制标记,如果填充,则启用按钮。

#1


1  

<asp:ImageButton OnClientClick="return ValidatorOnSubmit()" />

The ValidatorOnSubmit() function should already be included by the ASP.NET framework if you have validators on your form. The standard WebForm onsubmit function looks like this:

如果表单上有验证器,则ASP.NET框架应该已包含ValidatorOnSubmit()函数。标准WebForm onsubmit函数如下所示:

function WebForm_OnSubmit() {
   if (   typeof(ValidatorOnSubmit) == "function" 
       && ValidatorOnSubmit() == false) return false;

   return true;
}

#2


0  

There is an OnClientClick event you can set this to your javascript function. If you return true it will continue to the post back. If you return false the post back will not happen.

有一个OnClientClick事件,您可以将其设置为您的JavaScript函数。如果您返回true,它将继续回发。如果您返回false,则不会发回帖。

<asp:Button ID="NavigateAway" runat="server" OnClientClick="javascript:return PromptToNavigateOff();" OnClick="NavigateAwayButton_Click" Text="Go Away" />

  <script type="text/javascript">
    function PromptToNavigateOff()
    {
        return confirm("Are you sure you want to continue  and loose all your changes?");
    }

  </script>

#3


0  

I would simply reverse logic and not allow the user to click the button until he has filled the information. Put mandatory markers and if it is filled it then the button is enabled.

我只是简单地反转逻辑,不允许用户点击按钮,直到他填写了信息。放置强制标记,如果填充,则启用按钮。