简体   繁体   English

OnclientClick和OnClick不能同时工作?

[英]OnclientClick and OnClick is not working at the same time?

I have a button like the following, 我有一个类似以下的按钮,

<asp:Button ID="pagerLeftButton" runat="server" OnClientClick="disable(this)" onclick="pager_Left_Click" Text="<" />

When I use my button like that, onclick is not firing. 当我这样使用按钮时,onclick不会触发。 When I remove OnClientClick, then onclick is firing. 当我删除OnClientClick时,就会触发onclick。

What I need to do is, disable the button during the postback and enable it after the postback ends. 我需要做的是,在回发期间禁用该按钮,并在回发结束后启用它。

Edit: Additional information: 编辑:附加信息:

I added break point to my firing functions is c# part and I am debugging, they are not firing for sure. 我在我的触发函数中添加了断点,这是c#部分,我正在调试,但不能确定它们是否触发。 Those functions are like 这些功能就像

protected void pager_Left_Click(object sender, EventArgs e)
{
//Do smthing.
}
protected void pager_Right_Click(object sender, EventArgs e)
{
//Do smthing.
}

and when I click my button, it is disabled for 1-2 seconds and automatically enabled, but I am not sure why it is enabled. 当我单击我的按钮时,它会被禁用1-2秒并自动启用,但是我不确定为什么启用了它。 I didn't add any part for it to be enabled again. 我没有添加任何部分以使其再次启用。

From this article on web.archive.org : 来自web.archive.org上的这篇文章

The trick is to use the OnClientClick and UseSubmitBehavior properties of the button control. 诀窍是使用按钮控件的OnClientClick和UseSubmitBehavior属性。 There are other methods, involving code on the server side to add attributes, but I think the simplicity of doing it this way is much more attractive: 还有其他方法,包括在服务器端添加属性的代码,但是我认为以这种方式进行的简单性更具吸引力:

 <asp:Button runat="server" ID="BtnSubmit" OnClientClick="this.disabled = true; this.value = 'Submitting...';" UseSubmitBehavior="false" OnClick="BtnSubmit_Click" Text="Submit Me!" /> 

OnClientClick allows you to add client side OnClick script. OnClientClick允许您添加客户端OnClick脚本。 In this case, the JavaScript will disable the button element and change its text value to a progress message. 在这种情况下,JavaScript将禁用button元素并将其文本值更改为进度消息。 When the postback completes, the newly rendered page will revert the button back its initial state without any additional work. 回发完成后,新呈现的页面将把按钮还原回其初始状态,而无需进行任何其他工作。

The one pitfall that comes with disabling a submit button on the client side is that it will cancel the browser's submit, and thus the postback. 禁用客户端上的提交按钮的一个陷阱是它将取消浏览器的提交,并因此取消回发。 Setting the UseSubmitBehavior property to false tells .NET to inject the necessary client script to fire the postback anyway, instead of relying on the browser's form submission behavior. 将UseSubmitBehavior属性设置为false可以指示.NET注入必要的客户端脚本以无论如何触发回发,而不是依赖于浏览器的表单提交行为。 In this case, the code it injects would be: 在这种情况下,它注入的代码将是:

 __doPostBack('BtnSubmit','') 

This is added to the end of our OnClientClick code, giving us this rendered HTML: 这被添加到我们的OnClientClick代码的末尾,为我们提供了呈现的HTML:

 <input type="button" name="BtnSubmit" onclick="this.disabled = true; this.value ='Submitting...';__doPostBack('BtnSubmit','')" value="Submit Me!" id="BtnSubmit" /> 

This gives a nice button disable effect and processing text, while the postback completes. 在回发完成时,这将提供不错的按钮禁用效果并处理文本。

OnClientClick seems to be very picky when used with OnClick. 与OnClick一起使用时,OnClientClick似乎非常挑剔。

I tried unsuccessfully with the following use cases: 我尝试使用以下用例失败:

OnClientClick="return ValidateSearch();" 
OnClientClick="if(ValidateSearch()) return true;"
OnClientClick="ValidateSearch();"

But they did not work. 但是他们没有用。 The following worked: 以下工作:

<asp:Button ID="keywordSearch" runat="server" Text="Search" TabIndex="1" 
  OnClick="keywordSearch_Click" 
  OnClientClick="if (!ValidateSearch()) { return false;};" />

There are two issues here: 这里有两个问题:

Disabling the button on the client side prevents the postback 在客户端禁用按钮可防止回发

To overcome this, disable the button after the JavaScript onclick event. 为了解决这个问题,请在JavaScript onclick事件之后禁用该按钮。 An easy way to do this is to use setTimeout as suggested by this answer . 一种简单的方法是按照此答案的建议使用setTimeout

Also, the OnClientClick code runs even if ASP.NET validation fails, so it's probably a good idea to add a check for Page_IsValid . 此外,即使ASP.NET验证失败, OnClientClick代码也会运行,因此添加对Page_IsValid的检查可能是一个好主意。 This ensures that the button will not be disabled if validation fails. 这样可以确保如果验证失败,则不会禁用该按钮。

OnClientClick="(function(button) { setTimeout(function () { if (Page_IsValid) button.disabled = true; }, 0); })(this);"

It's neater to put all of this JavaScript code in its own function as the question shows: 如问题所示,将所有这些JavaScript代码放入自己的函数中比较整洁:

OnClientClick="disable(this);"

function disable(button) {
    setTimeout(function () {
        if (Page_IsValid)
            button.disabled = true;
    }, 0);
}

Disabling the button on the client side doesn't disable it on the server side 在客户端禁用按钮不会在服务器禁用它

To overcome this, disable the button on the server side. 要解决此问题,请禁用服务器端的按钮。 For example, in the OnClick event handler: 例如,在OnClick事件处理程序中:

OnClick="Button1_Click"

protected void Button1_Click(object sender, EventArgs e)
{
    ((Button)sender).Enabled = false;
}

Lastly, keep in mind that preventing duplicate button presses doesn't prevent two different users from submitting the same data at the same time. 最后,请记住,防止重复按下按钮并不能防止两个不同的用户同时提交相同的数据。 Make sure to account for that on the server side. 确保在服务器端解决此问题。

Vinay (above) gave an effective work-around. Vinay(上文)给出了有效的解决方法。 What's actually causing the button's OnClick event to not work following the OnClientClick event function is that MS has defined it where, once the button is disabled (in the function called by the OnClientClick event), the button "honors" this by not trying to complete the button's activity by calling the OnClick event's defined method. 实际上,导致按钮的OnClick事件在OnClientClick事件函数之后不起作用的原因是,MS已在按钮已禁用的位置(在OnClientClick事件调用的函数中)定义了它的位置,该按钮通过不尝试完成来“荣誉”它通过调用OnClick事件的定义方法来激活按钮的活动。

I struggled several hours trying to figure this out. 我费了好几个小时才能弄清楚。 Once I removed the statement to disable the submit button (that was inside the OnClientClick function), the OnClick method was called with no further problem. 一旦删除了禁用提交按钮的语句(位于OnClientClick函数内部),就可以调用OnClick方法,而不会出现其他问题。

Microsoft, if you're listening, once the button is clicked it should complete it's assigned activity even if it is disabled part of the way through this activity. Microsoft,如果您正在收听,则单击该按钮后,即使在此活动的一部分过程中被禁用,也应完成为其分配的活动。 As long as it is not disabled when it is clicked, it should complete all assigned methods. 只要单击它时未禁用它,它就应该完成所有分配的方法。

What if you don't immediately set the button to disabled, but delay that through setTimeout? 如果您不立即将按钮设置为禁用,而是通过setTimeout将其延迟怎么办? Your 'disable' function would return and the submit would continue. 您的“禁用”功能将返回,并且提交将继续。

Your JavaScript is fine, unless you have other scripts running on this page which may corrupt everything. 您的JavaScript很好,除非您在此页面上运行其他脚本可能会破坏所有内容。 You may check this using Firebug . 您可以使用Firebug进行检查。

I've now tested a bit and it really seems that ASP.net ignores disabled controls. 我现在已经测试了一下,似乎ASP.net确实忽略了禁用的控件。 Basically the postback is issued, but probably the framework ignores such events since it "assumes" that a disabled button cannot raise any postback and so it ignores possibly attached handlers. 基本上发布了回发,但是框架可能会忽略此类事件,因为它“假定”禁用的按钮无法引发任何回发,因此它会忽略可能附加的处理程序。 Now this is just my personal reasoning, one could use Reflector to check this in more depth. 现在,这只是我个人的推理,可以使用Reflector进行更深入的检查。

As a solution you could really try to do the disabling at a later point, basically you delay the control.disabled = "disabled" call using a JavaTimer or some other functionality. 作为解决方案,您实际上可以尝试在以后执行禁用操作,基本上可以使用JavaTimer或其他功能延迟control.disabled = "disabled"调用。 In this way 1st the postback to the server is issued before the control is being disabled by the JavaScript function. 以这种方式,在JavaScript函数禁用控件之前,先发布到服务器的回发。 Didn't test this but it could work 没测试过但是可以用

function UpdateClick(btn) { 函数UpdateClick(btn){

    for (i = 0; i < Page_Validators.length; i++) {

        ValidatorValidate(Page_Validators[i]);

        if (Page_Validators[i].isvalid == false)

            return false;
    }

    btn.disabled = 'false';

    btn.value = 'Please Wait...';

    return true;
}

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

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