简体   繁体   English

如果单击按钮,如何停止执行文本框事件更改的方法

[英]how to stop executing text box event changed methods if button clicked

im clearing all controls in textbox changed event but i need if after coming out from text box and directly clicking button . 即时通讯清除文本框中的所有控件已更改事件,但我需要从文本框出来后直接单击按钮。 reset/clear method should not be fired. 不应触发重置/清除方法。 i used java script to pass the value while clicking button . 我使用Java脚本在单击button时传递值。 value is not changing to true if button is clicked.button event is not firing if directly after text box clicking button. 如果单击按钮,则值未更改为true。如果直接在文本框单击按钮之后,则不会触发button事件。 if auto postback making true in textbox its working but i need with postbox true. 如果自动回发使文本框为真,其工作,但我需要与邮箱为真。

aspx.cs aspx.cs

 protected void txtRegno_TextChanged(object sender, EventArgs e)
    {

        if (HiddenField1.Value == "false") // No Button1_Click
        {
            Clear();
        }
        else // button1_click
        {
            HiddenField1.Value = "true"; // reset the hiddenfield1 value
        }


    }

javascript JavaScript的

 <script type="text/javascript">
    function setButtonClicked() {
        document.getElementById('<%= HiddenField1.ClientID %>').value = 'true';
    }
</script>

aspx button and text box aspx按钮和文本框

  <asp:TextBox ID="txtRegno" runat="server" OnTextChanged="txtRegno_TextChanged" AutoPostBack="true"
                MaxLength="10"></asp:TextBox>
  <asp:HiddenField ID="HiddenField1" runat="server" Value="false" />
            <asp:Button ID="btnSubmit" runat="server" Text="Check" 
            onclick="btnSubmit_Click" OnClientClick="setButtonClicked();"  />

I don't know that this is the "correct" answer, but this is what worked for me. 我不知道这是“正确”的答案,但这对我有用。

I created a global variable I called isSave and set it to false initially. 我创建了一个名为isSave的全局变量,并将其初始设置为false。 Then in the mouseover event, I set that variable to true. 然后在mouseover事件中,将该变量设置为true。 Like so... 像这样

var isSave = false;

$(document).ready(function () {
    $("#buttonDiv").on("mouseover",
        function () {
            window.onbeforeunload = null;
            isSave = true;
        });
    $("#buttonDiv").on("mouseout",
        function () {
            window.onbeforeunload = Changes;
            isSave = false;
        });
})

Then in the function that gets called onChange of the text field, I put a check for the value and returned that value. 然后,在称为文本字段的onChange的函数中,我检查了该值并返回了该值。

function txtExpChange(txt)
{
    // Do stuff

    return isSave;
}

If you need the function to not process anything just wrap it all in an if statement. 如果您需要函数不处理任何内容,只需将其包装在if语句中即可。

function txtExpChange(txt)
{
    if (!isSave)
    {
        // Do stuff
    }

    return isSave;
}

The gist is that since the mouseover occurs separately from the onchange event, it will set the variable independently. 要点是,由于鼠标悬停与onchange事件分开发生,因此它将独立设置变量。 The downside to this I suppose is if they hit the tab key whilst you are hovering over the div containing the buttons. 我想这样做的缺点是,当您将鼠标悬停在包含按钮的div上时,如果它们按下了tab键。

I hope there is a better solution out there, but until I find it this will have to work for me. 我希望那里有更好的解决方案,但是直到找到它,这对我来说还是行得通的。

Good Luck! 祝好运!

When you are focused in the textbox txtRegno , and then you click with mouse on the button, the OnTextChanged event is fired before the OnClientClick event, and therefore postback occures and the form is submitted with hidden field set to false , which is the initial value. 当您将焦点放在文本框txtRegno ,然后用鼠标单击按钮时,将在OnClientClick事件之前触发OnTextChanged事件,因此发生回发,并且表单的提交将隐藏字段设置为false ,这是初始值。

When you look at the source code of the rendered page, you will see that the OnTextChanged event and its handler is transformed into javascript change event which is fired after leaving the input box. 当您查看呈现页面的源代码时,您将看到OnTextChanged事件及其处理程序被转换为javascript change事件,该事件在离开输入框后被触发。

You will need to handle the change event of the text box on the client side by your self to do it. 您需要自己处理客户端上文本框的change事件。

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

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