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

retrieve client side id of selected row (DataKeyValue)

5 Answers 1559 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mario
Top achievements
Rank 2
Mario asked on 23 Jul 2009, 03:23 PM
Hi,

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 
                    if(id) 
                    { 
                        //oWnd.close(oArg); 
                        alert("docid = " + id); 
                    } 
                    else 
                    { 
                        alert("Please select a document."); 
                    } 
                } 
            </script> 
        </telerik:RadCodeBlock> 

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" 
                                            AllowPaging="True"  
                                            PageSize="20" 
                                            AutoGenerateColumns="False" 
                                            GridLines="Both" 
                                            AllowMultiRowSelection = "false" 
                                            ClientSettings-Selecting-AllowRowSelect = "true" 
                                            OnNeedDataSource="Radgrid_NeedDatasource"
                             
                            <MasterTableView DataKeyNames="id" 
                                        CommandItemDisplay="Top"
                                         
                                <CommandItemTemplate> 
                                    <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> 
                                    </div> 
                                </CommandItemTemplate> 
                                                                                     
                                <Columns> 
                                 
                                    <telerik:GridBoundColumn DataField="id" Visible="false"></telerik:GridBoundColumn> 
                                        <telerik:GridTemplateColumn> 
                                            <ItemTemplate> 
                                                <asp:Image runat="server" ID="imgNew" 
                                                    OnLoad="OnImageLoad" /> 
                                                <asp:TextBox ID="txbCreatedDate" runat="server" 
                                                    Visible ="false" 
                                                    Text='<%# DataBinder.Eval(Container.DataItem,"created_date").ToString() %>' /> 
                                            </ItemTemplate> 
                                        </telerik:GridTemplateColumn> 
                                        <telerik:GridHyperLinkColumn DataTextField="Metainformation.name" HeaderText="Profile" DataTextFormatString="{0}"></telerik:GridHyperLinkColumn> 
                                        <telerik:GridBoundColumn DataField="Category.name" 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="Folder.name" HeaderText="Ordner"></telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn DataField="id" UniqueName="id" Visible="false"></telerik:GridBoundColumn> 
                                         
                                    </Columns> 
                                       
                                    <RowIndicatorColumn> 
                                        <HeaderStyle Width="20px"></HeaderStyle> 
                                    </RowIndicatorColumn> 
 
                                    <ExpandCollapseColumn> 
                                        <HeaderStyle Width="20px"></HeaderStyle> 
                                    </ExpandCollapseColumn> 
                                </MasterTableView>           
                        </telerik:RadGrid> 


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

5 Answers, 1 is accepted

Sort by
0
Scott R
Top achievements
Rank 1
answered on 23 Jul 2009, 09:27 PM
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"  
 
                    tableView.deleteItem(i);  
                    items[i].get_element().style.display = 'none';  
 
                    itemcount = itemcount - 1;  
                }  
            }  
        }  
 
        tableView.clearSelectedItems();  
        tableView.set_virtualItemCount(itemcount);  
    }  
    catch (ex) {  
        alert('An error occurred while attempting to delete the selected items. Please refresh your browser and try again.');  
    }  
}  
 
0
Accepted
Princy
Top achievements
Rank 2
answered on 24 Jul 2009, 04:27 AM
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:
aspx:
<telerik:RadGrid ID="radGrid" runat="server" AllowPaging="True"   
                      PageSize="20" AutoGenerateColumns="False"  
                      GridLines="Both" AllowMultiRowSelection = "false"  
                      ClientSettings-Selecting-AllowRowSelect = "true"   
                      OnNeedDataSource="Radgrid_NeedDatasource">  
             <MasterTableView DataKeyNames="id" ClientDataKeyNames="id" 
                              CommandItemDisplay="Top">  

Thanks
Princy.
0
Mario
Top achievements
Rank 2
answered on 24 Jul 2009, 10:42 AM
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.



0
Francis Frank
Top achievements
Rank 1
answered on 25 Oct 2011, 11:00 AM
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
0
Jack
Top achievements
Rank 1
answered on 09 Mar 2016, 06:03 AM

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

Jack 

 

Tags
Grid
Asked by
Mario
Top achievements
Rank 2
Answers by
Scott R
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Mario
Top achievements
Rank 2
Francis Frank
Top achievements
Rank 1
Jack
Top achievements
Rank 1
Share this question
or