user48408
user48408

Reputation: 3354

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 somewhere 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?

Upvotes: 1

Views: 3229

Answers (2)

user48408
user48408

Reputation: 3354

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 I.e.

I.e. 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!!

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532435

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).

Upvotes: 4

Related Questions