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

ClientEvents-OnCommand and ItemCommand problem

15 Answers 543 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Legalia
Top achievements
Rank 1
Legalia asked on 14 May 2009, 09:13 AM
Hi,
i am facing on problem with RadGrid if i am using ClientEvents-OnCommand.
the problem is if i am adding ClientEvents-OnCommand in radgrid, it does not fire itemcomamnd event of grid for InitiInsert and cancel command.
if i remove it ClientEvents-OnCommand, it works fine.

15 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 18 May 2009, 07:08 AM
Hi Legalia,

This issue has already been fixed and the fix will be available in the next service pack for the controls.
Please, refer to my answer in ticket #212372.

All the best,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
bahgat
Top achievements
Rank 1
answered on 08 Sep 2010, 02:29 AM
i have the same problem
i use v 2010.1.519.35

this is my code

 function OnCommand(sender, args)
 {

            if (args.get_commandName() == "RebindGrid")
            {
                var tree = $find("<%=RadPanelBar2.ClientID%>").findItemByValue("the_value2").findControl("RadTreeView1");

                alert(tree.get_checkedNodes().length);
                if (tree.get_checkedNodes().length <= 0) {
                    args.set_cancel(true);
                }

        }
 }


when the commandName = "RebindGrid"  do postback  but  it does not fire itemcomamnd
         
but when the commandName =  any thing else it  do postback  and  fire itemcomamnd


and how can i get the ticket #212372.

thanks
0
Tsvetoslav
Telerik team
answered on 09 Sep 2010, 03:37 PM
Hello bahgat,

As I have pointed out in my last-year post: this issue was fixed quite a while ago. I tested the problematic scenario on my side with versioni 2010.1.519.35 and it works all right. Please,  open up a formal support ticket and send a runnable sample where the issues is reproduced. We shall debug it and get back to you with more information.

As for the #212372 ticket you need to look in the support communication under the Legalia Team account.

Hope this information helps.

Regards,
Tsvetoslav
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Romek Olearski
Top achievements
Rank 1
answered on 02 Feb 2011, 11:51 AM
Hi,
i am facing similar problem with RadGrid if i am using ClientEvents-OnCommand.
the problem is if i am adding ClientEvents-OnCommand in radgrid, it does fire itemcomamnd event of grid for user defined command,
but it always points to the first item in radgrid (e.Item) .


 .ascx

       <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">

            <script type="text/javascript">

                    function OnCommand(sender, args) {
                    // it is doing nothing
                }

            </script>

        </telerik:RadCodeBlock>



.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        this.RadGrid1.ItemCommand += OnItemCommand;
        this.RadGrid1.ClientSettings.ClientEvents.OnCommand = "OnCommand";
    }

    void OnItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.Item == null)
            return;

        switch (e.CommandName)
        {
            // user defined
            case "Standard":

                 GridDataItem item = (GridDataItem)e.Item;

                // e.g  id is always Id of the first item in radgrid if ClientEvents-OnCommand is defined
                var id = new Guid(item["Id"].Text);
... etc


if i remove ClientEvents-OnCommand, it works fine.


am i doing anything wrong ?
0
Tsvetoslav
Telerik team
answered on 07 Feb 2011, 03:20 PM
Hi Romek Olearski,

This behavior is by design when you have a client oncommand event wired up. Unless, however, you have a custom command argument in your command column, on the server in the ItemCommand event you will be getting for the CommandArgument property of the events object the grid row index. You can use the latter to find the grid's data item that has fired the command.

Hope it helps.

Best wishes,
Tsvetoslav
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Philipp
Top achievements
Rank 1
answered on 12 Jun 2014, 08:25 AM
Hi Tsvetoslav,

this behavior by design is a critical point in my project. Let me explain:

All i need is a client-event for pagesize_changed in radgrid. As far as i know the only way is the onCommand-Event in ClientEvents of Radgrid. Of course we have some code behind item_command-methods along with the grids we use, where we handle the command with the dataKeyValue of the source-Item. 

Now i need the check every coudeBehind item_command-event of our grids to fit them to the commandArgument(rowIndex) instead of dataKeyValue?
Why is there a rowindex, but not the source-item which fired the command?
Why is it always the first item?

Is there no other way to handle the pageSizeChanged_event clientside without the need to rework every codeBehind-event?
Is there a built in way to bind an client side event directly to pageSizeChanged?

Best wishes,
Philipp

0
Shinu
Top achievements
Rank 2
answered on 12 Jun 2014, 12:24 PM
Hi Philipp,

A suggestion is that you can subscribe to the OnCommand client event of the grid and check if the command name is Page and PageSize to accomplish your requirement.

ASPX:
<ClientSettings>
  <ClientEvents OnCommand="OnGridCommand"  />
</ClientSettings>

JS:
<script type="text/javascript">
    function OnGridCommand(sender, args) {    
        if (args.get_commandName() == "Page")
            alert("Page Index Changed");
        else if (args.get_commandName() == "PageSize")
            alert("Page Size Changed");
    }
</script>

Thanks,
Shinu
0
Philipp
Top achievements
Rank 1
answered on 12 Jun 2014, 12:47 PM
Hi Shinu,

you got me wrong. I have assigned a js-function to the ClientEvent: OnCommand, but this breaks up the whole server side ItemCommand Event of radgrid. Whenever i assign a js function to the client event, the server event always takes the first dataitem as event-source, which feels so wrong and makes every use of dataItem.getDataKeyValue() worthless. 

This would meen i have to overmake every ServerSide ItemCommand on every Page where i use it. And all that just because i need a global clientside event for pagesize_changed.

So my question is: Is there a way to assign a js-function to the clientevent pagSizeChanged, without the need to overmake all the server side ItemCommand Events.


Best wishes and thanks for you effort,
Philipp


0
Shinu
Top achievements
Rank 2
answered on 13 Jun 2014, 04:26 AM
Hi Philipp,

I guess you want to cancel the server side ItemCommand event from firing. You can use set_cancel in the client side function.

JS:
<script type="text/javascript">
    function OnGridCommand(sender, args) {
        if (args.get_commandName() == "Page") {
        //Your code
         args.set_cancel(true);// cancel event
        }
        else if (args.get_commandName() == "PageSize") {
         //Your code
          args.set_cancel(true);
        }
    }
</script>

Thanks,
Shinu
0
Philipp
Top achievements
Rank 1
answered on 13 Jun 2014, 07:07 AM
Hi Shinu,

i don't want to cancel anything.

I have server side code like this:

Protected Sub grid_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles grid.ItemCommand
   ...  
   CType(e.Item, GridDataItem).GetDataKeyValue("id")
   ...
End Sub
0
Philipp
Top achievements
Rank 1
answered on 13 Jun 2014, 07:09 AM
whenever i add a clientscript to the client-event onCommand of radgrid, my serverside code doesn't work anymore, because dataitem is always the first item in the grid-itemcollection. That is my problem! I don't understand why this is a behavior by design?

Best wishes
Philipp
0
Shinu
Top achievements
Rank 2
answered on 17 Jun 2014, 06:05 AM
Hi philipp,

Sorry that I misunderstood the issue you have mentioned. “When you assign an event handler to RadGrid's client-side OnCommand event, the grid uses another mechanism to postback and fire command events. When firing a custom command (excluding commands that invoke some action in RadGrid, e.g. Edit, Sort, Page, etc) with OnCommand handler attached, the server-side command handler's e.Item object does not represent the item that fired the command. Instead, the item index of the target item goes to e.CommandArgument. Thus, you would need to parse the index from the e.CommandArgument object and retrieve the item with the specified index from the e.Item.OwnerTableView.Items collection.”

ASPX:
<telerik:RadGrid ID="rgridEmployee" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDSEmployee" OnItemCommand="rgridEmployee_ItemCommand">
           <MasterTableView CommandItemDisplay="Top" DataKeyNames="EmployeeID" PageSize="2" AllowPaging="true"  >
               <Columns>
                   <telerik:GridBoundColumn DataField="EmployeeID" UniqueName="EmployeeID" HeaderText="EmployeeID">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="FirstName" UniqueName="FirstName" HeaderText="FirstName">
                   </telerik:GridBoundColumn>                  
                   <telerik:GridEditCommandColumn ></telerik:GridEditCommandColumn>
                   <telerik:GridButtonColumn CommandName="Test" Text="Test"></telerik:GridButtonColumn>
               </Columns>
           </MasterTableView>
           <ClientSettings>
               <ClientEvents OnCommand="onGridCommand" />             
           </ClientSettings>
       </telerik:RadGrid>
       <asp:SqlDataSource ID="SqlDSEmployee" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
           SelectCommand="SELECT  EmployeeID,FirstName FROM [Employees] "></asp:SqlDataSource>

JavaScript:
<script type="text/javascript">
    function onGridCommand(sender, args) {
    //some code    
    }
</script>

VB:-
Protected Sub rgridEmployee_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs)
    Dim employeeID As String = String.Empty
 
    If e.CommandName = RadGrid.EditCommandName Then
            ' Here I am getting correct item index
        employeeID = DirectCast(e.Item, GridDataItem).GetDataKeyValue("EmployeeID").ToString()
    End If
    If e.CommandName = "Test" Then
            ' Here the value is wrong as it is a custom command
        employeeID = DirectCast(e.Item, GridDataItem).GetDataKeyValue("EmployeeID").ToString()
    End If
End Sub

-Shinu.
0
Viktor Tachev
Telerik team
answered on 17 Jun 2014, 06:53 AM
Hello Philipp,

Would you elaborate extensively on the scenario you would like to implement? Would you like to implement only client-side logic for the page size changed event? Or you are firing a custom command from the client-side?

Please describe in detail what are your requirements and what should be the expected result.


Regards,
Viktor Tachev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Philipp
Top achievements
Rank 1
answered on 23 Jun 2014, 07:46 AM
Hi everybody and thank you for your suggestions,

@Shinu:
Ah okay, so the problem is that i have some custom commands, the built in commands work fine.
Than i have to redo all scenarios with cutom commands, if i want to use a global javascript for the onCommand-event of radgrid.

@Viktor Tachev:
my requirements are very simple, i want to pass the new pagesize-value clientside to an async webservice call. That's all.
If i have to redo every radgrid with custom commands because of such a simple thing, i stop right know and leave it behind.
I would love to have an own clientside event for 

Regards,
Philipp

0
Viktor Tachev
Telerik team
answered on 26 Jun 2014, 08:14 AM
Hello Philipp,

In case you would like to change the page size for the grid you could use the fireCommand() client side method. For example the following code would set the page size to 100:

function changeGridPageSize() {
    var grid = $find("<%= RadGrid1.ClientID %>");
    grid.get_masterTableView().fireCommand("PageSize", 100);
}

This said, if you are binding the RadGrid on the client you could use the RadClientDataSource control that provides an easy way to implement client-side binding for RadGrid. You could check out the functionality in this online demo.

Also please note that if you are using Virtualization for RadGrid and would like to get reference to a DataItem you could get the correct item index on the client.

Regards,
Viktor Tachev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Legalia
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
bahgat
Top achievements
Rank 1
Romek Olearski
Top achievements
Rank 1
Philipp
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Viktor Tachev
Telerik team
Share this question
or