Fire ItemCommand from client?

2 posts, 0 answers
  1. Raymond
    Raymond avatar
    27 posts
    Member since:
    Jul 2014

    Posted 10 Sep 2018 Link to this post

    Is there a way to fire the ItemCommand event from javascript the same as it would coming from a telerik control within a template item?  Using a telerik checkbox or other control inside a template, I can set the command name and argument.  When the server side itemcommand event fires, the event args parameter will have the command name, command argument, as well as the correct data item.

     

    I have an ASP checkbox that calls a javascript function to do some validation.  I then call the mastertable.fireCommand  function, which triggers the server side itemcommand event.  I'm able to pass the command and argument, but the data item in the event args is always index zero.  I've found a way to retrieve the index of the data item on the client, and pass that index as the commandargument.  But I'd like to be able to use the commandargument parameter for something else.  

     

                function CheckClick(sender, Idx) {
                    debugger;
                    var masterTable = $find("<%= rgGrid.ClientID %>").get_masterTableView();
                    var row = masterTable.get_dataItems()[Idx];
                    masterTable.fireCommand("Post", Idx);
                }

     

    I could combine the index and additional data and send it together in the command argument, so I do have a workaround.  But since the telerik controls are able to pass all three, I assume there should be a better way.  I expected something like a fireCommand function of the data item, but I'm not finding anything.  How do the telerik controls do that?

     

     

     

  2. Eyup
    Admin
    Eyup avatar
    3644 posts

    Posted 13 Sep 2018 Link to this post

    Hi Raymond,

    While the corresponding FireCommandEvent method on server-side fires the command using the given GridDataItem instance:
    https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/control-lifecycle/how-to-fire-command-events

    The client-side equivalent fireCommand() method uses the first item of the MasterTableView to do that:
    https://docs.telerik.com/devtools/aspnet-ajax/controls/listview/client-side-programming/radlistview/methods/firecommand

    Therefore, you can use one of these options to achieve this requirement:

    1. Enable the AutoPostBack of the controls and execute your logic directly on server-side.
    2. Use a custom separator string to split the arguments string in the code-behind (as you've already mentioned)
    3. Use JSON to stringify an object to pass as a single string argument and then deserialize it on server-side. A sample similar to this suggestion can be found in the sample provided in the last post here:
    https://www.telerik.com/support/code-library/get-selected-items-through-all-pages#1eTU8nr-GUG8zfGgOGbIcA
    4. Initiate AJAX request using the template control:
    https://docs.telerik.com/devtools/aspnet-ajax/controls/ajaxmanager/client-side-programming/overview#ajaxrequestwithtargeteventtarget-eventargument
    And on code-behind check the initiator ID whether ends with the expected ID of the control:
    https://www.telerik.com/support/kb/aspnet-ajax/details/determine-postback-control-or-ajax-request-initiator

    I hope this will prove helpful.

    Regards,
    Eyup
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top