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

RadGrid hiearchy Get ParentID on click of <CommandItemTemplate> Button

1 Answer 65 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 09 Oct 2018, 03:21 PM

I have a radgrid hierarchy structure.  When the user navigates to the third level and clicks on the parent tickmark on the grid, the RadGrid1_ItemCommand is fired and I can retrieve the parent "ProgramID". 

But I need to pickup the parentID when the user clicks on the command-template button located just on top of the detail rows.  I would like to get the value within a client-side function or server-side if that is easier??? (but cannot get that to work well).  See attachment for a jpg of grid...

 

Thank you for you help with this request!

 

Client-Side code:

function openConfirmationWindowNEW(sender, args) {

    var programID= parentItem.OwnerTableView.Items[sender.Item.ItemIndex].GetDataKeyValue("ProgramID");          

    radopen("Storyboard.aspx?carID=programID, "RadWindow2");

}

 

Server-Side Code:

console.write((string) sender.Item.OwnerTableView.Items[e.Item.ItemIndex].GetDataKeyValue("ProgramID").ToString());

 

The code works below but it needs it to work from within a client-side function....

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {

            if (e.Item.OwnerTableView.DataSourceID == "SqlDataSource3")
            {
                GridDataItem parentItem = (GridDataItem)(e.Item.OwnerTableView.ParentItem);
                if (parentItem != null)
                {
                    //..tier 2 parent
                    Console.Write(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["StrategicInitID"]);

                    //..tier 3 parent
                    HiddenProgramIDSelected.Value = (string)e.Item.OwnerTableView.Items[e.Item.ItemIndex].GetDataKeyValue("ProgramID").ToString();

                }
            }
    }

    

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 10 Oct 2018, 06:10 PM
Hi John,

Let's assume the following CommandItemTemplate in any of the DetailTables (any level). The button in it is bound to the ClientClicked event and the handler is commandItemClick 
<CommandItemTemplate>
    <telerik:RadButton ID="RadButton3" runat="server" Text="RadButton" OnClientClicked="commandItemClick" AutoPostBack="false"></telerik:RadButton>
</CommandItemTemplate>

The commandItemClick event handler
function commandItemClick(sender, args) {
    var ParentTableView = sender.get_parent()
    var ParentOfParentTableView = ParentTableView.get_parentView();
    // instantiate the dataitems collection. This is required to be able to cast a <tr> element into a dataItem (see next step)
    ParentTableView.get_parentView().get_dataItems();
    // Parent Row HTML element
    var parentRowElement = ParentTableView.get_parentRow();
    // cast the HTML row element to DataItem by calling the .control method
    var parentDataItem = ParentTableView.get_parentRow().control;
 
    var parentRowDataKeyID = parentDataItem.getDataKeyValue("FieldNameOfID"); // NOTE: In order to access a Raw value in the getDataKeyValue() method, this field must be specified in the ClientDataKeyNames collection of the TableView.
}

E.g.
<DetailTables>
    <telerik:GridTableView DataKeyNames="ID" ClientDataKeyNames="ID" Name="ChildGrid" CommandItemDisplay="Top">
        <CommandItemSettings ShowAddNewRecordButton="false" />
        <CommandItemTemplate>
            <telerik:RadButton ID="RadButton3" runat="server" Text="RadButton" OnClientClicked="commandItemClick" AutoPostBack="false"></telerik:RadButton>
        </CommandItemTemplate>
    </telerik:GridTableView>
</DetailTables>

Attached you can find an example of a similar scenario.

Kind regards,
Attila Antal
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or