Get parent Grid ClientID on click event from Toolbar inside CommandItemTemplate

3 posts, 0 answers
  1. Massimiliano
    Massimiliano avatar
    185 posts
    Member since:
    Oct 2012

    Posted 14 Dec 2013 Link to this post

    Ok, I hope the topic title is self explanatory, anyway... I have RadToolbar inside a CommandItemTemplate of a RadGrid.
    I have a client event tied to the RadToolbar on click (wich checks if grid items are selected otherwise triggers a js alert).
    Since I want to use the same function for the multiple grids I have on the page I cannot use $find("<%=GridName.ClientID%>") inside the onclick function. A workaround would be to bind different functions, where I just define the $find("<%=GridName.ClientID%>") for each grid and then  call the main function. But a neater solution would be to retrieve GridName.ClientID inside the function just from JS without resorting to inline code.
    I tryed mangling with the "sender" object (wich is the toolbar triggering the click event) in the function to try to get the parent grid clientID but with no luck.
    Any hint?
    The structure is this:
    <telerik:RadGrid runat="server" ID="GridName"
            <telerik:RadToolBar OnClientButtonClicking="TheFunction"

    function TheFunction(sender, args) {
    var myGrid = $find("<%=GridName.ClientID%>"); // TO BE RESOLVED WITHOUT EXPLICIT REFERENCE WITH <% %>

    Any hint?...

  2. Angel Petrov
    Angel Petrov avatar
    1044 posts

    Posted 18 Dec 2013 Link to this post

    Hi Massimiliano,

    Please try the following:
    function TheFunction(sender,args) {
                var myGrid = $find(Telerik.Web.UI.Grid.GetFirstParentByTagName(sender.get_element().parentNode, "div").id);
    The above shown code finds the first div which wraps the CommandItemTemplate. With the below shown markup that div should be the grid.

    Angel Petrov
    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 the blog feed now.
  3. Massimiliano
    Massimiliano avatar
    185 posts
    Member since:
    Oct 2012

    Posted 18 Dec 2013 Link to this post

    Wow that was hard! :D thanks Angel...
    Now that I'm slowly beginning to get a bit more confident with RadGrid I was thinking about writing a small "tutorial" on how to access various grid elements (command item and stuff inside, filters, templates and stuff inside, columns, rows, cells, data in cells, and everything else) both server side and client side, from the different grid events and outside of grid events.
    I think this would be an awesome reference. Something is in your help guide/reference but I think it's not put in this organic and schematic way.
    So for beginners it would be great to have a reference like "How do I access the radtoolbar inside commanditem template on my button click?" or "How do I do the same thing during grid item created event?" or "from JS on my client button click?" and so on.
Back to Top