I have a 2 level hierarchial RadGrid. Each row in the MasterTable will have an ItemTemplate column with RadTextbox in it. When the users enters a value in a textbox and then tabs out (blur) I want to be able to accomplish looping through all the childrows in client side script to extract values for those children only. I'm trying to understand how to match up a MasterTable row with gridtableview in the grid.get_detailTables() collection. Each parent row will only ever have 1 child table. Right now I'm just fudging it by saying that there is a 1 to 1 relationship between the # of parent rows & the # of detailTables, so the index of any parent row is equal to the index of the matching detail table. I'm sure this is a bad assumption. Here's what I have so far, but I'm not sure if it's fool proof:
ASPX
VB
Next, I think I may need something like below. The equivalent of the inner loop is what I really need help converting to client script. Please help. Thanks!
ASPX
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="HGridScript.aspx.vb" Inherits="HGridScript" %> |
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
<html xmlns="http://www.w3.org/1999/xhtml"> |
<head runat="server"> |
<title></title> |
</head> |
<body> |
<form id="form1" runat="server"> |
<telerik:RadScriptManager ID="RadScriptManager1" runat="server"> |
</telerik:RadScriptManager> |
<div> |
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" |
GridLines="None"> |
<MasterTableView ClientDataKeyNames="Something" HierarchyLoadMode="Client"> |
<Columns> |
<telerik:GridTemplateColumn HeaderText="Text1" UniqueName="Text1"> |
<ItemTemplate> |
<telerik:RadTextBox ID="txt1" runat="server" ></telerik:RadTextBox> |
</ItemTemplate> |
</telerik:GridTemplateColumn> |
<telerik:GridBoundColumn DataField="Something" HeaderText="Something"></telerik:GridBoundColumn> |
</Columns> |
<DetailTables> |
<telerik:GridTableView Name="Level2" > |
<Columns> |
<telerik:GridTemplateColumn HeaderText="Text2" UniqueName="Text2"> |
<ItemTemplate> |
<telerik:RadTextBox ID="txt2" runat="server" ></telerik:RadTextBox> |
</ItemTemplate> |
</telerik:GridTemplateColumn> |
<telerik:GridTemplateColumn HeaderText="ext" UniqueName="ext"> |
<ItemTemplate> |
<telerik:RadTextBox ID="lblExt" runat="server" BorderColor="Transparent" ></telerik:RadTextBox> |
</ItemTemplate> |
</telerik:GridTemplateColumn> |
<telerik:GridBoundColumn DataField="SomethingElse" HeaderText="Something Else"></telerik:GridBoundColumn> |
</Columns> |
</telerik:GridTableView> |
</DetailTables> |
<RowIndicatorColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</RowIndicatorColumn> |
<ExpandCollapseColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</ExpandCollapseColumn> |
</MasterTableView> |
</telerik:RadGrid> |
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="GridScript()" /> |
</div> |
<script type="text/javascript"> |
function getItem(index) { |
var grid = $find("<%=RadGrid1.ClientID %>") |
var item = grid.get_masterTableView().get_dataItems()[index]; |
var lvl2Total = 0; |
var lvl2Items = grid.get_detailTables()[index].get_dataItems(); |
for (var i = 0; i < lvl2Items.length; i++) { |
var txt2 = lvl2Items[i].findControl("txt2"); |
lvl2Total += Number(txt2.get_value()); |
} |
alert(lvl2Total); |
} |
</script> |
</form> |
</body> |
</html> |
VB
Imports System.Data |
Imports Telerik.Web.UI |
Partial Class HGridScript |
Inherits System.Web.UI.Page |
Private Function CreateTable(ByVal ColumnName As String, ByVal Value As String) As DataTable |
Dim dt As New DataTable |
Dim c As New DataColumn(ColumnName) |
dt.Columns.Add(c) |
Dim r As DataRow = dt.NewRow |
r(0) = Value |
dt.Rows.Add(r) |
r = dt.NewRow |
r(0) = Value & " 2" |
dt.Rows.Add(r) |
Return dt |
End Function |
Protected Sub RadGrid1_DetailTableDataBind(ByVal source As Object, ByVal e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles RadGrid1.DetailTableDataBind |
Select Case e.DetailTableView.Name |
Case "Level2" |
e.DetailTableView.DataSource = CreateTable("SomethingElse", "else Something") |
End Select |
End Sub |
Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource |
Me.RadGrid1.DataSource = CreateTable("Something", "some Data") |
End Sub |
Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender |
For Each item As GridDataItem In Me.RadGrid1.MasterTableView.Items |
CType(item("Text1").Controls(1), RadTextBox).Attributes.Add("onblur", "getItem('" & item.ItemIndex.ToString & "');") |
Next |
End Sub |
End Class |
Next, I think I may need something like below. The equivalent of the inner loop is what I really need help converting to client script. Please help. Thanks!
For Each item As GridDataItem In Me.RadGrid1.MasterTableView.Items |
For Each child As GridDataItem In item.ChildItem.NestedTableViews(0).Items |
Next |
Next |