简体   繁体   English

onClientClick返回Function()仍会触发OnClick

[英]onClientClick return Function() still fires OnClick

Trying to prevent my Button1_Click from submitting my data after testing if its valid in javascript. 尝试阻止Button1_Click在javascript中测试数据有效后提交我的数据。 Have tried a bunch of times but it submits regardless of what I return in the function. 尝试了很多次,但是无论我在函数中返回什么,它都会提交。

<form runat="server">
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" OnClientClick="return testValidate();" />
</form>

Button1_Click is ac# function that currently only alerts me if it is called - Edit Button1_Click是ac#函数,当前仅在被调用时提醒我-编辑

My Javascript function: 我的Javascript函数:

function testValidate() { 
return false;
}

I have tried a few proposed solutions such as using OnClientClick="if (!testValidate()) return 我已经尝试了一些建议的解决方案,例如使用OnClientClick =“ if(!testValidate())return
false;" 假;”

However none of these have worked 但是这些都不起作用

Thanx for the help in advance 感谢预先的帮助

It is hard to understand what you really are looking for, but if you do not want your form data to be submitted when validation fails then you would need to convert it all to JavaScript and setup an ajax call (this would require removing the OnClick event), or do all your validation in the C# code function, which you could then use a bool to determine whether or not to submit the data. 很难理解您真正在寻找什么,但是如果您不希望在验证失败时提交表单数据,则需要将其全部转换为JavaScript并设置ajax调用(这将需要删除OnClick事件),或在C#代码函数中进行所有验证,然后可以使用布尔值确定是否提交数据。

A JavaScript example: 一个JavaScript示例:

function testValidate() { 
    var is_valid = false;
    // Do validation code
    if(is_valid) {
        $..ajax({
           url: //URL location of your webservice,
           method: 'POST',
           contentType: "application/json; charset=utf-8",
           dataType: 'json',
           data: //Form data,
           success: function (data) {

           },
           error: function (data, text, error) {
              alert(error);
           }
        });
     }
}

The web service method Web服务方法

[WebMethod]
public void SubmitData(/*Lay out all parameters here*/) 
{
     //Insert form data
}

Doing it all in the OnClick. 在OnClick中完成所有操作。 (Requires removing the OnClientClick as it will no longer be needed) (需要删除OnClientClick,因为将不再需要它)

Front side: 前面:

<form runat="server">
    <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
</form>

C#: C#:

protected void Button1_Click(object sender, EventArgs e) 
{
     bool isValid = false;
     // Do validation
     if(isValid)
     {
         // submit form data
     }
}

I apologize in advance if I misunderstand what you are looking for and I am willing to revise my answer if what I am giving now is incorrect for your needs. 如果我误解了您的期望,我向您致歉,如果我现在给出的信息不符合您的需求,我愿意修改答案。 If it is not what you are looking for please clarify your question. 如果不是您要找的东西,请澄清您的问题。 Thanks! 谢谢!

EDIT: 编辑:

Here is why OnClientClick="return false;" 这就是为什么OnClientClick="return false;" and OnClientClick="return testValidate();" OnClientClick="return testValidate();" are different. 是不同的。

JS will return the second it sees a return statement, regardless of whether or not the code is finished executing. 无论代码是否完成执行,JS都会在看到return语句的第二秒返回。 So return testValidate(); 所以return testValidate(); executes a function call and returns immediately not giving testValidate() a chance to finish running where as return false; 执行一个函数调用并立即返回,不给testValidate()一个完成return false;运行的机会return false; returns false immediately. 立即返回false。

To avoid returning immediately try just using OnClientClick="var test = testValidate(); setTimeout(function() { return test; }, 10);" 为避免立即返回,请尝试仅使用OnClientClick="var test = testValidate(); setTimeout(function() { return test; }, 10);" . Or you can put inside the timeout time limit (the 10 in this example) any amount of time in milliseconds to allow for the return to only happen once the function is done and test has a value. 或者,您可以将超时时间限制(在本示例中为10)置于毫秒内,以使返回仅在函数完成并且测试具有值之后才发生。 Or you can just do OnClientClick="testValidate();" 或者,您可以只做OnClientClick="testValidate();" which will evaluate testValidate() as a boolean after it has called it which will allow the return false; 调用后,将把testValidate()评估为布尔值,这将允许return false; in your function to be called and false to be returned. 在您的函数中被调用而返回false I hope that makes more sense. 我希望这更有意义。

instead of 代替

OnClientClick="return testValidate();"

just write 写吧

OnClientClick="testValidate();"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM