Run ActionResult AND javascript function from Custom ToolBar button

2 posts, 0 answers
  1. Alex
    Alex avatar
    75 posts
    Member since:
    May 2015

    Posted 07 Apr 2016 Link to this post

    I am using an MVC grid that has a toolbar, with a custom button

    toolBar.Custom().Text("Recalculate Assets").Action("Edit", "AssetPlanning", new { recalculate = true }).HtmlAttributes(new { title = "Warning: This could take multiple minutes to fully complete", data_toggle = "tooltip", id = ("calcButton" + uniqueName), onClientClick = "alert('reclacing');" });


    This actionresult can take upto a minute to run. I would at the very least like to update the button to say calculating with a little spinner and whatnot


    var buttonID = "#calcButton" & String(@uniqueName.ToString());
    $(buttonID).click(function () { recalculating(); });
    function recalculating() {
        $(buttonID).addClass("btn btn-lg btn-warning");
        $(buttonID).html('<span class="glyphicon glyphicon-refresh glyphicon-refresh-animate"></span> Recalculating');


    I would think that somewhere along the way the function would get called or an alert box would show, but nothing appears on the client side. I'm guessing that the action gets called first and the client click event just is never called. What can I do to make the client code called first or run together?

  2. Dimiter Topalov
    Dimiter Topalov avatar
    1047 posts

    Posted 11 Apr 2016 Link to this post

    Hello Alex,

    The code sample suggests that there is a JavaScript error thrown, and the click event handler is not attached at all. In the following line:

    var buttonID = "#calcButton" & String(@uniqueName.ToString());

    ... a bitwise operator (&) is incorrectly used on string operands, resulting in a 0 (zero) being assigned to the buttonID variable. This causes a JavaScript error on the next line, as $(0) is not a valid selector.

    You can find more information about operators in JavaScript and their corresponding operands in the following reference:

    Please check the developers tools console for JavaScript errors, and fix them.

    I hope this helps, but let us know, if I am missing something, or you need further assistance.

    Dimiter Topalov
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top