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

Get parent Grid ClientID on click event from Toolbar inside CommandItemTemplate

2 Answers 288 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Massimiliano
Top achievements
Rank 1
Massimiliano asked on 14 Dec 2013, 02:00 PM
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 asp.net 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"
  <MasterTableView
     <CommandItemTemplate>
        <telerik:RadToolBar OnClientButtonClicking="TheFunction"
....

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

Any hint?...


2 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 18 Dec 2013, 01:25 PM
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.

Regards,
Angel Petrov
Telerik
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.
0
Massimiliano
Top achievements
Rank 1
answered on 18 Dec 2013, 04:06 PM
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.
Tags
Grid
Asked by
Massimiliano
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Massimiliano
Top achievements
Rank 1
Share this question
or