Friday, December 27, 2013

LinkButton OnClick Not Firing After OnClientClick

I worked around an oddball problem where the OnClick event of an ASP LinkButton would not fire following execution of some JavaScript tied to its OnClientClick.

The intent is to enable a row in a Telerik RadGrid to be removed by clicking the corresponding button in the row, and more specifically have a modal confirmation dialog appear to prompt the user to confirm their choice.

Here's markup for the GridTemplateColumn containing the button:
<telerik:GridTemplateColumn HeaderText="Remove" UniqueName="Remove" 
  FilterControlWidth="20">
    <ItemTemplate>
        <div style="cursor: hand;">
            <asp:LinkButton runat="server" ID="_RemoveItem" 
                Name="_RemoveItem" Text=" X " Font-Underline="False" 
                ForeColor="DarkBlue" 
                OnClientClick="confirmAspButton(this, 'Are you sure?'); return false;"
                OnClick="RemoveItemButtonClick" 
                style="font-weight:bold;color:red;">
            </asp:LinkButton>
        </div>
    </ItemTemplate>
    <HeaderStyle Width="55"></HeaderStyle>
</telerik:GridTemplateColumn>

The confirmAspButton function is a little JavaScript which is bound to the OnClientClick method of the LinkButton. If the user clicks OK, the confirmation passes and a postback will occur to execute the code beneath the OnClick method, whereas if they click Cancel the dialog will simply disappear.
     function confirmAspButton(button, message) 
     {
          function aspButtonCallbackFn(arg) 
          {
             if (arg) 
             {
                  window.__doPostBack(button.name, "");
             }
          }
            
          window.radconfirm(message, aspButtonCallbackFn, 330, 120, null, "Confirm");
     }


Seems simple enough, yet for some odd reason, in Internet Explorer 10, the LinkButton brought up the dialog just fine, and although the Cancel button click performed as expected, clicking OK had the same effect as cancel and merely cleared the dialog without executing the underlying code.

I managed to find a workaround via StackOverflow which suggests using a Button rather than a LinkButton. Sure enough, this did the trick, for whatever oddball reason whose particulars I could care less to dive into...