retrieve client side id of selected row (DataKeyValue)

6 posts, 1 answers
  1. Mario
    Mario avatar
    75 posts
    Member since:
    Jun 2009

    Posted 23 Jul 2009 Link to this post


    i got a grid in a radwindow and i want to return the id of the selected row dataitem.
    The id is in the MAsterView DataKeyvalue and i also tried to put it in a invisible column with unique name id..
    I want to do it client side with java script as i already have used JS by opening the Radwindow.

    After i read the references in the online documentary i got following code:

    <telerik:RadCodeBlock ID="radCodeBlock" runat="server"
                <script type="text/javascript"
                    function GetRadWindow() 
                       var oWindow = null
                       if (window.radWindow) oWindow = window.radWindow; 
                       else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow; 
                       return oWindow; 
                    function returnToParent() 
                        //create the argument that will be returned to the parent page 
                        var oArg = new Object(); 
                        //get the selected id from RadGrid 
                        var grid = $find("<%=radGrid.ClientID %>"); 
                        var MasterTable = grid.get_masterTableView(); 
                        var selectedRows = MasterTable.get_selectedItems(); 
                        for (var i = 0; i < selectedRows.length; i++) 
                          alert(i); // got 0 for 1 selectec row 
                          var row = selectedRows[i]; 
                          var cell = MasterTable.getCellByColumnUniqueName(row, "id"
                        var id = cell.innerHtml(); 
                        //var id = $find("<%=radGrid.ClientID %>").get_masterTableView().get_selectedItems()[0].getDataKeyValue("id"); 
                        //get a reference to the current RadWindow 
                        var oWnd = GetRadWindow(); 
                        //Close the RadWindow and send the argument to the parent page 
                            alert("docid = " + id); 
                            alert("Please select a document."); 

    With this i get an page error that "null is Null, or not an object". Its also not working with sell.innerHtml  without ().
    i also used this inline outcommented way/line, but then i come to the false alert.

    Here the grid:

    <telerik:RadGrid ID="radGrid" runat="server" 
                                                AllowMultiRowSelection = "false" 
                                                ClientSettings-Selecting-AllowRowSelect = "true" 
                                <MasterTableView DataKeyNames="id" 
                                        <div style="padding: 5px 5px;"
                                            <img onclick="returnToParent()" style="border:0px;vertical-align:middle;" alt="" src="../App_Themes/Default/Images/file_icon.gif"/> 
                                            <asp:Label ID="lblChooseDocument" runat="server" Text="choosedocument" OnLoad="OnLabelLoad"></asp:Label> 
                                        <telerik:GridBoundColumn DataField="id" Visible="false"></telerik:GridBoundColumn> 
                                                    <asp:Image runat="server" ID="imgNew" 
                                                        OnLoad="OnImageLoad" /> 
                                                    <asp:TextBox ID="txbCreatedDate" runat="server" 
                                                        Visible ="false" 
                                                        Text='<%# DataBinder.Eval(Container.DataItem,"created_date").ToString() %>' /> 
                                            <telerik:GridHyperLinkColumn DataTextField="" HeaderText="Profile" DataTextFormatString="{0}"></telerik:GridHyperLinkColumn> 
                                            <telerik:GridBoundColumn DataField="" HeaderText="Kategorie"></telerik:GridBoundColumn> 
                                            <telerik:GridBoundColumn DataField="User.lastname" HeaderText="Ersteller"></telerik:GridBoundColumn> 
                                            <telerik:GridBoundColumn DataField="created_date" HeaderText="Erstellungsdatum"></telerik:GridBoundColumn> 
                                            <telerik:GridBoundColumn DataField="User1.lastname" HeaderText="letzter Bearbeiter"></telerik:GridBoundColumn> 
                                            <telerik:GridBoundColumn DataField="checkout_date" DataType="System.DateTime" HeaderText="in Verwendung seit"></telerik:GridBoundColumn> 
                                            <telerik:GridBoundColumn DataField="" HeaderText="Ordner"></telerik:GridBoundColumn> 
                                            <telerik:GridBoundColumn DataField="id" UniqueName="id" Visible="false"></telerik:GridBoundColumn> 
                                            <HeaderStyle Width="20px"></HeaderStyle> 
                                            <HeaderStyle Width="20px"></HeaderStyle> 

    The radwindow works fine and the grid has mutli select disabled and row select allowed

    Any ideas why im not able to retrieve the id of the selected row?

    cheers mario
  2. Scott R
    Scott R avatar
    63 posts
    Member since:
    Jun 2008

    Posted 23 Jul 2009 Link to this post

    This should help you out:

    function DeleteSelectedAccounts() {  
        try {  
            var grid = GetCustomerGrid();  
            var tableView = grid.get_masterTableView();  
            var items = tableView.get_dataItems();  
            var itemcount = tableView.get_virtualItemCount();  
            if (items.length > 0) {  
                var i = 0;  
                for (i = 0; i < items.length; i++) {  
                    if (items[i].get_selected()) {  
                        var id = items[i].getDataKeyValue('Id');  
                        // Do stuff with "id"  
                        items[i].get_element().style.display = 'none';  
                        itemcount = itemcount - 1;  
        catch (ex) {  
            alert('An error occurred while attempting to delete the selected items. Please refresh your browser and try again.');  
  3. DevCraft R3 2016 release webinar banner
  4. Answer
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 23 Jul 2009 Link to this post

    Hello Mario,

    To retrieve the key values for a row client side, you would have to set the ClientDataKeyNames property for the GridTableView. Make sure that you set the ClientDataKeyNames to the required key data fields as shown below:
    <telerik:RadGrid ID="radGrid" runat="server" AllowPaging="True"   
                          PageSize="20" AutoGenerateColumns="False"  
                          GridLines="Both" AllowMultiRowSelection = "false"  
                          ClientSettings-Selecting-AllowRowSelect = "true"   
                 <MasterTableView DataKeyNames="id" ClientDataKeyNames="id" 

  5. Mario
    Mario avatar
    75 posts
    Member since:
    Jun 2009

    Posted 24 Jul 2009 Link to this post

    thx princy, its working with "your" grid propertie and my "inline" javascript approach:

    var id = $find("<%=radGrid.ClientID %>").get_masterTableView().get_selectedItems()[0].getDataKeyValue("id");

    thx mario.

  6. Francis Frank
    Francis Frank avatar
    35 posts
    Member since:
    Mar 2010

    Posted 25 Oct 2011 Link to this post

    Please I need some help here friends!

    I have a grid and 3 linkbuttons inside it then i want to trigger seperate events when each of the butttons are clicked not when the row is selected.
    I have been able to get the buttons respond on sepearate clicks but i could retrieve any value from that row neither could I acces the clientdatakeynames of the grid

    I will be glad to have a response

    waiting sirs ...
    Thanks in anticipation
  7. Jack
    Jack avatar
    1 posts
    Member since:
    Jan 2015

    Posted 09 Mar in reply to Princy Link to this post

    Hey Princy, thank you and was it for me!! 



Back to Top
DevCraft R3 2016 release webinar banner