简体   繁体   中英

OnClick Event not Firing

I've created a very simple test page to replicate a problem I'm having within my application.Sorry if this looks long winded for something so trivial.

The .aspx page

<body>
<form id="form1" runat="server">
    <asp:TextBox ID="tb" runat="server"></asp:TextBox>
    <asp:Button ID="btnOk" runat="server" text="OK" Width="60px"  UseSubmitBehavior="true" OnClientClick="saveAmendments();" />
</form>

In the page_load in the code behind I add an onchange attribute to the textbox

if (!IsPostBack)
    {
        tb.Attributes.Add("onchange", "addSaveDetails('abc###123###456###test###abc###');");
    }
}

Which gets rendered as follows

<body>
    <form name="form1" method="post" action="TestJP.aspx" id="form1">
    <div>
        <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUi3er8ht8oaCZzy2..." />
    </div>

    <div>
       <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwKOoa..." />
    </div>

    <input name="tb" type="text" value="testtt" id="tb" onchange="addSaveDetails('abc###123###456###test###abc###');" />
    <input type="submit" name="btnOk" value="OK" onclick="saveAmendments();" id="btnOk" style="width:60px;" />
</form>

So if i change the value in the text box and click somwhere else on the screen the AddSaveDetails function fires. Then click on the button and the saveAmendments fires. However if you change the text and then click immediately on the button (rather than somewhere else on the screen first) and only the first (onchange) function fires. The onClick event never does. The js being called does nothing other than an alert. What could be stopping the event bubbling up to the last call?

Try returning true from your functions in addition to the alert. I presume that the event action propagation is being stopped because you are returning false (or nothing, which is being interpreted as false).

Infuriating. Thanks tvanfosson. It got me on the right track but I don't need to explicitly return true

In my js function I had a debugger; statement. That was screwing things up Ie

Ie Commenting out debugger fixed things..

function addSaveDetails(strDetails) {
debugger;
alert('func1');
//return true;

}

What is it about the debugger; that prevents the 2nd event firing! Argh. It turns out i actually created a problem trying to solve a non-existent one!!

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