简体   繁体   English

火 OnClick 取决于 OnClientClick 的结果

[英]fire OnClick depends on the result of OnClientClick

I have some legacy codes, some implemented in JavaScript and some implemented in C#.我有一些遗留代码,一些在 JavaScript 中实现,一些在 C# 中实现。 Now I have to write a piece of code to decide whether or not to fire OnClick depends on some conditions.现在我必须编写一段代码来决定是否触发 OnClick 取决于一些条件。 I want to implement it by checking the condition in the OnClientClick.我想通过检查 OnClientClick 中的条件来实现它。 I tried some methods from the Internet, but they did not work.我尝试了一些来自互联网的方法,但它们没有奏效。 (I'm new to JavaScript and C#, so I'm not really understand the solutions, for example, setting OnClientClick="return confirm ('This will delete the report. Continue?'); return false; ", doesn't it always return before executing "return false"?") (我是 JavaScript 和 C# 的新手,所以我不是很了解解决方案,例如设置 OnClientClick="return confirm ('这将删除报告。继续?'); return false; ",不它总是在执行“return false”之前返回?”)

my codes are as follow我的代码如下

aspx file:
<div id="searchBtn"><asp:LinkButton ID="SearchButton" OnClientClick="return SetInputString(); return false;" OnClick="SearchClick" ValidationGroup="search"  runat="server"><img src="images/btn_search.jpg" alt="検索" /></asp:LinkButton></div>

js file:
function SetInputString() {
    var textbox = document.getElementById("textForm");
    var inputString = document.getElementById("inputString");
    clearNonInteractiveTimer();
    inputString.value = textbox.value;

    if (/^[0-9]{6}$/.test(inputString.value)) {
        EvaluateEnteredSearch(inputString.value);
        return false;
    } else {
        return true;
    }
}

cs file:
public void SearchClick(object sender, EventArgs e){
...
}

Confirm is a method "built-in" into the Javascript. Confirm是 Javascript 中“内置”的方法。

This method is modal: it stops the execution of scripts and prevents the user from interacting with the rest of the page until the window is closed.此方法是模态的:它停止执行脚本并阻止用户与页面的 rest 交互,直到 window 关闭。

So no, the output will depend on the user's choice.所以不,output 将取决于用户的选择。

return false means that you are canceling the default action of the pressed button. return false表示您正在取消按下按钮的默认操作。

There are two ways to tell the browser that you want to cancel the default action for some event, in this case, cancel the following link: call event.preventDefault () or return false from the handler function.有两种方法可以告诉浏览器您要取消某些事件的默认操作,在这种情况下,取消以下链接:调用event.preventDefault ()或从处理程序 function return false The second only works if you assign a handler using the onevent attribute like so:仅当您使用onevent属性分配处理程序时,第二个才有效,如下所示:

<a href="//google.com" onclick="return false"> google </a>

Or like this:或者像这样:

<a href="//google.com"> google </a>

<script>
document.getElementsByTagName ('a') [0] .onclick = function () {
  return false;
}
</script>

More info here: How to undo a default action此处的更多信息:如何撤消默认操作


Now, what about ur code.现在,你的代码呢。

OnClick Event sends a request to the server and performs an action in response. OnClick事件向服务器发送请求并执行操作作为响应。

OnClientClick is very similar to the onclick function we use to write in Javascript... that is, it executes statements on the client side without sending them to the server. OnClientClick与我们在 Javascript 中编写的 onclick function 非常相似......也就是说,它在客户端执行语句而不将它们发送到服务器。

SetInputString will be called first, if true - SearchClick will be called SetInputString将首先被调用,如果为true - SearchClick将被调用

Also, more info here: when to call the Server onClick vs OnClientClick另外,这里有更多信息: 何时调用服务器 onClick 与 OnClientClick

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

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