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

How to find ID of RadGrid?

6 Answers 284 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rick
Top achievements
Rank 1
Rick asked on 24 Oct 2008, 09:38 AM
We have a RadGrid linked up to a ContextMenu to give the user options when a row on the RadGrid is clicked.

The code is as follows:

<ClientSettings>
            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
            <ClientEvents OnRowContextMenu="RowContextMenu"></ClientEvents>
            <Resizing EnableRealTimeResize="True" />
</ClientSettings>   

And our on page JavaScript is:

function RowContextMenu(sender, eventArgs)
    {
        var menu = $find("<%= RadMenu1.ClientID %>");
        var evt = eventArgs.get_domEvent();                  
        
        var index = eventArgs.get_itemIndexHierarchical();
        document.getElementById("radGridClickedRowIndex").value = index;
        
        sender.get_masterTableView().selectItem(sender.get_masterTableView().get_dataItems()[index].get_element(), true);
        
        menu.show(evt);
        
        evt.cancelBubble = true;
        evt.returnValue = false;

        if (evt.stopPropagation)
        {
           evt.stopPropagation();
           evt.preventDefault();
        }
    }

I'm trying to move this javascript to an external function, but to do that I need to either pass in the RadGrid ID or I need to retrieve it from the Sender object to replace the first line.

Any ideas?



6 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 24 Oct 2008, 11:00 AM
Hello Rick,

You can use $find("<%= RadGrid1.ClientID %>"); similar to the code for RadMenu1.

Sincerely yours,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rick
Top achievements
Rank 1
answered on 24 Oct 2008, 11:07 AM
Hi,

Thanks for the reply but as you can see from my code above that IS currently how we obtain the ID.

I'm trying to get the MenuID, not the GridID, sorry my mistake.

I'm trying to generalise the code and so don't want to have to refer to a "RadMenu1" like it does currently.

Can I not pull out the ID from the sender or eventArgs parameters? Or can I pass the ID as a parameter to the function?
0
Vlad
Telerik team
answered on 24 Oct 2008, 11:13 AM
Hi Rick,

Since the context menu is not part of the grid (actually it is a separate control on your page) you cannot get the menu ID from the grid client-side event handler argumnets.

Kind regards,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rick
Top achievements
Rank 1
answered on 24 Oct 2008, 12:31 PM
Hi,

Thanks for that, it makes sense.

Is there any way I can pass that ID as a parameter in the RowContextMenu function? Otherwise I'm going to have to have that function on every single page rather than just have it in an external js file.
0
Dimo
Telerik team
answered on 24 Oct 2008, 01:10 PM
Hello Rick,

You cannot pass additional parameters to the RowContextMenu() function, however, another thing that you can do is to assign the RadMenu's Client ID to a global javascript variable and use that variable inside an external Javascript function. For example:


ASPX:

<script type="text/javascript">

var RadMenu1ClientID = "<%= RadMenu1.ClientID %>";

</script>


External JS:

function RowContextMenu(sender, eventArgs)
{
    var menu = $find(RadMenu1ClientID);

    ..........
}



Greetings,
Dimo
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rick
Top achievements
Rank 1
answered on 24 Oct 2008, 03:45 PM
Thanks for the tip. In the end I've written a wrapper function for the .aspx page and hidden the rest of it in an external file


<script type="text/javascript">  

    function RowContextMenu(sender, eventArgs) { CustomRowContextMenu(sender, eventArgs, "<%= RadMenu1.ClientID %>", "radGridClickedRowIndex") }    
            
</script>
       
Not as neat but a big improvement
Tags
Grid
Asked by
Rick
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Rick
Top achievements
Rank 1
Dimo
Telerik team
Share this question
or