Accessing controls in RadListView DataGroupTemplate

3 posts, 1 answers
  1. Bubacarr
    Bubacarr avatar
    4 posts
    Member since:
    Aug 2011

    Posted 24 Jun 2015 Link to this post

    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. Answer
    Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1803 posts

    Posted 29 Jun 2015 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Bubacarr
    Bubacarr avatar
    4 posts
    Member since:
    Aug 2011

    Posted 29 Jun 2015 in reply to Konstantin Dikov Link to this post

    Thank you Konstantin!
Back to Top