简体   繁体   English

在onclientclick之后停止onclick

[英]Stop onclick after onclientclick

I've seen a few examples on how to do this but they don't seem to be working for me. 我已经看到了一些有关如何执行此操作的示例,但它们似乎对我没有用。 Having said that, I am doing it a little differently than the examples I've seen so I'm not sure if what I'm trying to do is possible. 话虽如此,我做的事情与我所看到的例子有些不同,所以我不确定我想做的事情是否可行。

I have a multiline asp texbox and onclientclick I want to make sure (among other things) the user hasn't gone over on max length before I submit the onclick event. 我有一个多行asp texbox和onclientclick,我想确保(在其他情况下)用户在提交onclick事件之前没有超过最大长度。 However, this textbox is part of a user control that will be used X number of times on the page so I can't just grab the control from the Javascript. 但是,此文本框是用户控件的一部分,在页面上将被使用X次,因此我不能只是从Javascript中获取控件。 I have to send the clientID from the code behind. 我必须从后面的代码发送clientID。 So I'm adding the OnClientClick event on the codebehind and pass the clientID for the control there. 因此,我在后面的代码上添加了OnClientClick事件,并在那里传递了该控件的clientID。 I wonder if that's why I'm getting the results I'm getting. 我不知道这就是为什么我要得到结果。

SaveNoteButton.OnClientClick = string.Format("return BeforeSave('{0}');", NoteTextBox.ClientID);

<asp:Button runat="server" CssClass="casenotes-bluebuttons" ID="SaveNoteButton" Text="Save" OnClick="SaveNoteButton_Click" Enabled="false" />

    function BeforeSave(noteCtrl) {
        var txt = document.getElementById(noteCtrl);

        if (txt.value.length > 500) {
            alert("false");
            return false;
        }
        else {
            alert("true");
            return true;
        }
    }

So in theory, the OnClientClick property is added to the SaveNoteButton button. 因此,从理论上讲,OnClientClick属性将添加到SaveNoteButton按钮。 When it's fired, it passes the NoteTextBox.ClientID, the js checks the textbox length, returns true or false then the OnClick event fires depending on the return value. 当它被触发时,它传递NoteTextBox.ClientID,js检查文本框长度,返回true或false,然后根据返回值触发OnClick事件。 But it doesn't. 但事实并非如此。 I even tried wrapping the method call in an alert and the method is in fact returning what I expect but the OnClick event isn't firing regardless of the method's return value. 我什至尝试将方法调用包装在警报中,并且该方法实际上返回了我期望的值,但是无论该方法的返回值如何,均不会触发OnClick事件。 I even tried removing the method call and hardcoding true and it still doesn't fire. 我什至尝试删除方法调用并将其硬编码为true,但仍然不会触发。 So I know the return value is true and yet no OnClick love. 因此,我知道返回值是真实的,但没有OnClick喜欢。

Oddly enough, it was syntax on the call to the js method. 奇怪的是,这是调用js方法的语法。

SaveNoteButton.OnClientClick = string.Format("return BeforeSave('{0}');", NoteTextBox.ClientID);

becomes 变成

SaveNoteButton.OnClientClick = string.Format("BeforeSave('{0}')", NoteTextBox.ClientID);

and it works just fine. 而且效果很好。

Remove the Enabled="false" bit. 删除Enabled="false"位。 This is why the onclick event does not fire. 这就是为什么onclick事件不会触发的原因。

好了,这是我为解决问题所做的:

SaveNoteButton.OnClientClick = string.Format("if(!BeforeSave('{0}', '{1}')) return false;", NoteTextBox.ClientID, this._maxLength);

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

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