Why is a button Click event also called when a textbox TextChanged event is called???

Discussion in 'ASP .Net' started by S_K, Nov 7, 2007.

  1. S_K

    S_K Guest

    This is a weird problem..

    I have a page with a textbox and a button (along with a bunch of other
    stuff). Both the textbox and the button have their SEPERATE events
    defined for the textchanged and click events. When I change the text
    in the textbox the textchanged event is called as expected. But the
    button click event is also called!?

    Whats with that?!

    Can anybody help? This is driving me crazy!


    S_K, Nov 7, 2007
    1. Advertisements

  2. S_K

    bruce barker Guest

    possible reasons

    1) you hit enter/return after changing the text. this will cause the
    button to postback

    2) you don't have autopostback set on the textbox, so the postback only
    occurs when you hit the button.

    -- bruce (sqlwork.com)
    bruce barker, Nov 7, 2007
    1. Advertisements

  3. S_K

    S_K Guest

    First, Thanks for responding.

    Second, The first possiblility makes sense. However, why would hitting
    the <ENT> or return key cause a button event to fire? Is there any way
    I can undo that?

    Thanks again.
    S_K, Nov 7, 2007
  4. S_K

    LVP Guest

    <script language=javascript type=text/javascript>
    <!-- Script courtesy of http://www.web-source.net - Your Guide to
    Professional Web Site Design and Development
    function stopRKey(evt) {
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement
    : null);
    if ((evt.keyCode == 13) && (node.type=="text")) {return false;}

    document.onkeypress = stopRKey;

    From http://www.web-source.net/javascript_disable_enter_key.htm by John
    LVP, Nov 7, 2007
  5. S_K

    LVP Guest

  6. S_K

    LVP Guest


    LVP, Nov 7, 2007
  7. If you are using .NET 2.0, you can set the Button's UseSubmmitBehavior
    property to false.
    Robert Dunlop, Nov 8, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.