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

getting the datakeyvalue

6 Answers 361 Views
TreeList
This is a migrated thread and some comments may be shown as answers.
shahab
Top achievements
Rank 1
shahab asked on 06 Dec 2010, 01:05 PM
Hi I am using the following code to display self referencing heirarchy and it is working fantastic. My problem is when I enable paging and go to the second page or any other page then I cannnot get the datakeyvalue for the row and it crashes.
This is the markup I am using  :
<telerik:RadTreeList runat="server" ID="tvwApp" AllowPaging="true" 
        DataKeyNames="AppID" OnItemCommand="RadTreeList1_ItemCommand" AutoGenerateColumns="false"
        OnPageSizeChanged="RadTreeList1_PageSizeChanged" OnPageIndexChanged="RadTreeList1_PageIndexChanged"
        ParentDataKeyNames="HistoricalappID" Skin="Windows7" EnableEmbeddedSkins="true"
        ItemStyle-Height="30px" AlternatingItemStyle-Height="30px" AllowSorting="true"
        HeaderStyle-Height="30px" AlternatingItemStyle-BackColor="WhiteSmoke" 
            GridLines="Horizontal">
<AlternatingItemStyle BackColor="WhiteSmoke" Height="30px"></AlternatingItemStyle>
  
<ItemStyle Height="30px"></ItemStyle>
        <Columns>
            <telerik:TreeListBoundColumn HeaderText="Reference No" DataField="AppRefNo" >
                <HeaderStyle Width="100px"></HeaderStyle>
            </telerik:TreeListBoundColumn>
            <telerik:TreeListBoundColumn HeaderText="Applicant" DataField="LOTRPNo" HeaderStyle-Width="35%" >
                  
            </telerik:TreeListBoundColumn>
            <telerik:TreeListBoundColumn HeaderText="UDA" DataField="ULDA">
            </telerik:TreeListBoundColumn>
            <telerik:TreeListBoundColumn SortExpression="DatePropMade" HeaderText="Date Properly Made"
                DataField="DatePropMade" DataFormatString="{0:d}">
            </telerik:TreeListBoundColumn>
            <telerik:TreeListBoundColumn SortExpression="DateDecision" HeaderText="Decisione Due"
                DataField="DateDecision" DataFormatString="{0:d}">
            </telerik:TreeListBoundColumn>
            <telerik:TreeListBoundColumn SortExpression="StatusName" HeaderText="Status" DataField="StatusName" UniqueName="StatusName">
            </telerik:TreeListBoundColumn>
            <telerik:TreeListBoundColumn SortExpression="IsPNRequired" HeaderText=" " DataField="IsPNRequired" UniqueName="IsPNRequired" HeaderStyle-Width="0px" ItemStyle-Width="0px" ItemStyle-Font-Size="1px">
<HeaderStyle Width="0px"></HeaderStyle>
  
<ItemStyle Font-Size="1px" Width="0px"></ItemStyle>
            </telerik:TreeListBoundColumn>
            <telerik:TreeListTemplateColumn HeaderStyle-Width="60px" HeaderText="Edit">
                <ItemTemplate>
                    <telerik:RadButton ID="SelectButton" runat="server" CommandName="Edit" Text="Edit"
                        ItemStyle-CssClass="edit" Width="50px" />
                </ItemTemplate>
  
<HeaderStyle Width="60px"></HeaderStyle>
            </telerik:TreeListTemplateColumn>
            <telerik:TreeListTemplateColumn HeaderStyle-Width="60px" HeaderText="Copy">
                <ItemTemplate>
                    <telerik:RadButton ID="RadButton1" runat="server" CommandName="Copy" Text="Copy"
                        ItemStyle-CssClass="copy" Width="50px" />
                </ItemTemplate>
  
<HeaderStyle Width="60px"></HeaderStyle>
            </telerik:TreeListTemplateColumn>
        </Columns>
  
<HeaderStyle Height="30px"></HeaderStyle>
    </telerik:RadTreeList>

and the code behind is :
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
  
        If Not IsPostBack Then
            tvwApp.DataSource = LoadData(txtSearch.Text)
            tvwApp.DataBind()
            tvwApp.ExpandToLevel(1)
            txtSearch.Focus()
            CreateMenu()
            btnAddNew.Text = "Add New Application"
  
            DisplayAppDetailsHeader()
        End If
  
    End Sub
  
    Public Sub RadTreeList1_ItemCommand(ByVal sender As Object, ByVal e As TreeListCommandEventArgs) Handles tvwApp.ItemCommand
  
        If e.CommandName = RadTreeList.ExpandCollapseCommandName Then
            tvwApp.DataSource = LoadData(txtSearch.Text)
            tvwApp.DataBind()
        End If
  
        If (e.Item.ItemType = TreeListItemType.Item Or e.Item.ItemType = TreeListItemType.AlternatingItem) Then
            Dim item As TreeListDataItem = DirectCast(e.Item, TreeListDataItem)
            Dim APPID As String = item.OwnerTreeList.DataKeyValues(item.DataItemIndex)("AppID").ToString()
  
            If e.CommandName = "Copy" Then
                Response.Redirect(_apppath + "copy.aspx?ID=" & APPID & "&tabId=tab1&Action=PREAPP")
            ElseIf e.CommandName = "Edit" Then
                Response.Redirect(_apppath + "process.aspx?ID=" & APPID & "&tabId=tab1&Action=PREAPP")
            End If
        End If
    End Sub
  
  
Protected Sub RadTreeList1_PageIndexChanged(ByVal source As Object, ByVal e As Telerik.Web.UI.TreeListPageChangedEventArgs)
        tvwApp.CurrentPageIndex = e.NewPageIndex
        tvwApp.DataSource = LoadData(txtSearch.Text)
        tvwApp.DataBind()
    End Sub
    Protected Sub RadTreeList1_PageSizeChanged(ByVal source As Object, ByVal e As Telerik.Web.UI.TreeListPageSizeChangedEventArgs)
        tvwApp.DataSource = LoadData(txtSearch.Text)
        tvwApp.DataBind()
    End Sub

This is the error that get when I go to page 2 or any other page except page 1
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
  
'error occurs on  this line
  Dim APPID As String = item.OwnerTreeList.DataKeyValues(item.DataItemIndex)("AppID").ToString()

any help would be most appreciated.
many thanks

6 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 06 Dec 2010, 01:54 PM
Hello shahab,

You need to use the item's DisplayIndex, not the DataSetIndex:

Dim APPID As String = item.OwnerTreeList.DataKeyValues(item.DisplayIndex)("AppID").ToString()

Alternatively, you can access the data key values directly from the item:

Dim APPID As String = item.GetDataKeyValue("AppID").ToString()

Also, note that for advanced data operations like paging/sorting, it is recommended that you use advanced RadTreeList databinding through its NeedDataSource event. Thus, you ensure RadTreeList's data source is provided whenever needed and only in one place. You can refer to the SImple vs Advanced RadTreeList Binding help topic for more info.

Veli
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
de Phoenix
Top achievements
Rank 1
answered on 19 Jan 2011, 09:11 AM
Hi! 
How do I get the value of the datakey when the OnItemClick event is executed? When I click on item of TreeList I want to load data in RadGrid, but SQL statement of RadGrid using datakey value of TreeList. Is it possible?
0
Veli
Telerik team
answered on 19 Jan 2011, 10:35 AM
You need to enable postback on item click for RadTreeList:

<telerik:RadTreeList ID="RadTreeList1" runat="server"
    OnItemCommand="RadTreeList1_ItemCommand">
    <ClientSettings AllowPostBackOnItemClick="true">
        <Selecting AllowItemSelection="true" />
    </ClientSettings>
</telerik:RadTreeList>

Now, every time you click on a treelist item, RadTreeList will postback and fire its ItemCommand event with the ItemClick  command name. We can use that to get the data key value of the clicked item and rebind RadGrid:

protected void RadTreeList1_ItemCommand(object sender, TreeListCommandEventArgs e)
{
    if (e.CommandName == "ItemClick")
    {
        TreeListDataItem treeListItem = (TreeListDataItem)e.Item;
        object dataKey = treeListItem.GetDataKeyValue("DataKeyField");
        //bind RadGrid with dataKey here
    }
}

The above ItemCommand event handler gets the data key value of the clicked item. You can then use it to bind the RadGrid. Note that if you need to AJAX-ify this scenario, you either have to put both RadTreeList and RadGrid in a single RadAjaxPanel, you define the following setting if using RadAjaxManager:

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadTreeList1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>

Greetings,
Veli
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
de Phoenix
Top achievements
Rank 1
answered on 19 Jan 2011, 11:32 AM
Thanks so much for quick responding! It was very helpful!
0
soraya
Top achievements
Rank 1
answered on 21 Jun 2012, 07:52 PM
hi!!! how can i get tha datakeyvalue on client side on OnItemSelected???
0
Princy
Top achievements
Rank 2
answered on 22 Jun 2012, 04:08 AM
Hi Soraya,

Try setting ClientDataKeyNames Property of RadTreelist and get the value using following Javascript.

JS:
<script type="text/javascript" >
    function OnItemClick(sender, args)
    {
        var val = args.get_item().get_dataKeyValue("id");
    }
</script>

Hope this helps.

Thanks,
Princy.
Tags
TreeList
Asked by
shahab
Top achievements
Rank 1
Answers by
Veli
Telerik team
de Phoenix
Top achievements
Rank 1
soraya
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or