This is a migrated thread and some comments may be shown as answers.

Fire ItemCommand from client?

1 Answer 396 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Raymond
Top achievements
Rank 1
Raymond asked on 10 Sep 2018, 11:07 PM

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?

 

 

 

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 13 Sep 2018, 06:15 AM
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.
Tags
Grid
Asked by
Raymond
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or