简体   繁体   中英

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 . value is not changing to true if button is clicked.button event is not firing if directly after text box clicking button. if auto postback making true in textbox its working but i need with postbox true.

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

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

aspx button and text box

  <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. Then in the mouseover event, I set that variable to 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.

function txtExpChange(txt)
{
    // Do stuff

    return isSave;
}

If you need the function to not process anything just wrap it all in an if statement.

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. The downside to this I suppose is if they hit the tab key whilst you are hovering over the div containing the buttons.

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.

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.

You will need to handle the change event of the text box on the client side by your self to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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