Getting at the GridCommandItem via the client-side API

5 posts, 1 answers
  1. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 12 Mar 2012 Link to this post

    Normally, if I need a client-side reference to the grid's CommandItem, I wire up the Toolbar's OnClientLoad command and save the reference when that fires. Either that or use $find(...).

    However, I find myself updating a page that used multiple instances of a user control that has a grid in it and i need to access the CommandItem for a grid when a row on the grid is selected, but for the life of me I can't find anything in the API that allows me to get at the CommandItem from, say, the OnRowSelecting event.

    Is this possible?

    --
    Stuart
  2. jumpstart
    jumpstart avatar
    479 posts
    Member since:
    Nov 2011

    Posted 14 Mar 2012 Link to this post

    Stuart:

    I'm not sure I totally understand your requirement, but the code provided in this CodeLibrary may provide some assistance:

    Client-side calculations (reflected in RadGrid's command item) for selected rows

    "The example illustrates how to update a label residing in the command item template of RadGrid purely client-side to reflect the cumulative unit price from the selected records in the table. For this purpose we intercept the OnRowSelected and OnRowDeselected events of the control to increment/decrement the current sum..."

    Hope it helps!
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 14 Mar 2012 Link to this post

    Jumpstart,

    Thanks for looking at this, sadly, I don't think that's going to help me.

    I'll try explaining the situation a bit better.

    I have a UserControl. The UserControl contains a RadGrid and the RadGrid has a CommandItem Template that includes a RadToolBar.

    I need to access one or more of the buttons in the RadToolBar when the user clicks on a row in the grid.

    Normally, this wouldn't be an issue. I would wire up the OnClientLoad event of the RadToolBar and, when it fires, store a reference to it where I can access it in Javascript later on...

    function OnGridRowSelecting(sender, e)
    {
      //gridCommandItem is a variable that I populate during the
      // RadToolBar's OnClientLoad event
      var buttons = gridCommandItem.get_items();
      // ...
    }

    in this specific case, however, I have multiple instances of my UserControl on the page, so, ideally, what I'd like to do is something like this...
    function OnGridRowSelecting(sender, e)
    {
      var commandItem = sender.get_someCleverMethodThatAllowsMeToAccessTheComamndItem()
      var toolbar = $telerik.findControl(commandItem, "RadToolBar1");
      var buttons = toolbar.get_items();
      // ...
    }

    Does that make any clearer what it is I'm trying to do?

  5. Answer
    Martin
    Admin
    Martin avatar
    811 posts

    Posted 19 Mar 2012 Link to this post

    Hello Stuart,

    The approach you have followed (using the Telerik static client library) is correct. Unfortunately, RadGrid's client side object does not provide API for the command item template. When rendered on the page this item is just a table row element that has "rgCommandRow" class attribute. You can use jQuery for example to find this element on the page. My suggestion however, is to pass the master table view element to the findControl method instead:

    function OnGridRowSelecting(sender, e)
    {
      //var commandItem = sender.get_someCleverMethodThatAllowsMeToAccessTheComamndItem()
      var toolbar = $telerik.findControl(sender.get_masterTableView().get_element(), "RadToolBar1");
      var buttons = toolbar.get_items();
      // ...
    }

    I hope this helps.

    Greetings,
    Martin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  6. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 19 Mar 2012 Link to this post

    Nice one.

    Thanks martin.

    --
    Stuart
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017