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

Accessing controls in RadListView DataGroupTemplate

2 Answers 133 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Bubacarr
Top achievements
Rank 1
Bubacarr asked on 24 Jun 2015, 07:15 PM

I am trying to access div tags within DataGroupTemplate from ItemDataBound but nothing is coming up for me. Not sure what i'm missing or might I be taking the wrong approach here?

 

<DataGroups>
    <telerik:ListViewDataGroup GroupField="Level" DataGroupPlaceholderID="DataGroupPlaceHolder1">
 
        <DataGroupTemplate>
            <div id="Div1" runat="server">
                <div id="Div2" runat="server">
                    <span class="GroupText"><%# CType(Container, RadListViewDataGroupItem).DataGroupKey%></span>
                </div>
                <asp:PlaceHolder runat="server" ID="DataGroupPlaceHolder2"></asp:PlaceHolder>
            </div>
        </DataGroupTemplate>
 
    </telerik:ListViewDataGroup>
</DataGroups>
 
 
Protected Sub RadListView1_ItemDataBound(sender As Object, e As RadListViewItemEventArgs) Handles RadListView1.ItemDataBound
 
    If e.Item.ItemType = RadListViewItemType.DataItem Then
        Dim dataitem As RadListViewDataItem = CType(e.Item, RadListViewDataItem)  
        If dataitem IsNot Nothing Then
            Dim grpdiv As HtmlGenericControl = TryCast(e.Item.FindControl("Div1"), HtmlGenericControl)
            If grpdiv IsNot Nothing Then
                'Do something
            End If
        End If 
    End If
 
End Sub

 

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 29 Jun 2015, 12:25 PM
Hello Bubacarr,

Please refer to the answer in the support ticket that you have opened regarding the same issue: ticket ID 947255.

For other with similar requirement convenience, below is the answer from the support ticket:

"Since the control that you are trying to access is within the DataGroupItem, your IF condition should handle that item type and not the DataItem type:
Protected Sub RadListView1_ItemDataBound(sender As Object, e As RadListViewItemEventArgs) Handles RadListView1.ItemDataBound
    If e.Item.ItemType = RadListViewItemType.DataGroupItem Then
        Dim dataitem As RadListViewDataGroupItem = CType(e.Item, RadListViewDataGroupItem)
        If dataitem IsNot Nothing Then
            Dim grpdiv As HtmlGenericControl = TryCast(e.Item.FindControl("Div1"), HtmlGenericControl)
            If grpdiv IsNot Nothing Then
                'Do something
            End If
        End If
    End If
  
End Sub

I have tested the above code with the following example and the DIV control is found correctly:
<telerik:RadListView ID="RadListView1" runat="server" OnItemDataBound="RadListView1_ItemDataBound"
    OnNeedDataSource="RadListView1_NeedDataSource1" ItemPlaceholderID="DataGroupPlaceHolder2">
    <LayoutTemplate>
        <asp:PlaceHolder runat="server" ID="DataGroupPlaceHolder1"></asp:PlaceHolder>
    </LayoutTemplate>
    <ItemTemplate>
        test
    </ItemTemplate>
    <DataGroups>
        <telerik:ListViewDataGroup GroupField="Level" DataGroupPlaceholderID="DataGroupPlaceHolder1">
            <DataGroupTemplate>
                <div id="Div1" runat="server">
                    <div id="Div2" runat="server">
                        <span class="GroupText"><%# CType(Container, RadListViewDataGroupItem).DataGroupKey%></span>
                    </div>
                    <asp:PlaceHolder runat="server" ID="DataGroupPlaceHolder2"></asp:PlaceHolder>
                </div>
            </DataGroupTemplate>
        </telerik:ListViewDataGroup>
    </DataGroups>
</telerik:RadListView>

And the code-behind:
Protected Sub RadListView1_NeedDataSource1(sender As Object, e As RadListViewNeedDataSourceEventArgs)
    Dim table As New DataTable()
    table.Columns.Add("ID", GetType(Integer))
    table.Columns.Add("Level", GetType(Integer))
    For i As Integer = 0 To 4
        table.Rows.Add(i, 5)
        table.Rows.Add(i, -5)
    Next
  
    TryCast(sender, RadListView).DataSource = table
End Sub
  
Protected Sub RadListView1_ItemDataBound(sender As Object, e As RadListViewItemEventArgs) Handles RadListView1.ItemDataBound
    If e.Item.ItemType = RadListViewItemType.DataGroupItem Then
        Dim dataitem As RadListViewDataGroupItem = CType(e.Item, RadListViewDataGroupItem)
        If dataitem IsNot Nothing Then
            Dim grpdiv As HtmlGenericControl = TryCast(e.Item.FindControl("Div1"), HtmlGenericControl)
            If grpdiv IsNot Nothing Then
                'Do something
            End If
        End If
    End If
End Sub

If any further assistance is needed, I would suggest that we continue our conversation in the support ticket.
 

Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Bubacarr
Top achievements
Rank 1
answered on 29 Jun 2015, 01:00 PM
Thank you Konstantin!
Tags
ListView
Asked by
Bubacarr
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Bubacarr
Top achievements
Rank 1
Share this question
or