access control in GroupTemplate

3 posts, 1 answers
  1. David
    David avatar
    38 posts
    Member since:
    May 2013

    Posted 09 Dec 2013 Link to this post

    i have a requirement to access a Label control that sits in the GroupTemplate.
    the issue for me, resides in the ItemDataBound event of the RadListView.

    when i FindControl for "lblFileName", which sits in the ItemTemplate, it finds it OK.
    but when i FindControl for "lblHeader", which sits in the GroupTemplate, it can't find it.

    my desire is to have a different Group Header Label for each individual Group. This Label will be programatically created so it is not possible to know what it would be before the Page Loads.





    My HTML is:
    <telerik:RadListView ItemPlaceholderID="plItemHolder"  GroupPlaceholderID="plGroupHolder" runat="server" ID="lstCSV">
     
                <LayoutTemplate>
                    <asp:PlaceHolder runat="server" ID="plGroupHolder"></asp:PlaceHolder>    
                </LayoutTemplate>
                
                <GroupTemplate>
                      <asp:Label ID="lblHeader" runat="server" Text="<h3>hello world</h3>"></asp:Label>
                      <asp:PlaceHolder runat="server" ID="plItemHolder"></asp:PlaceHolder>           
                </GroupTemplate>
     
                <ItemTemplate>
                    <table>
                        <tr>
                            <td>
                                <asp:Label runat="server" ID="lblFileName"></asp:Label>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
     
            </telerik:RadListView>


    i am then calling the following Method to Bind to my RadListView

    Private Sub getFileNames()
     
        Dim di As New DirectoryInfo(Server.MapPath(uploadCSV.TargetFolder))
        Dim diar1 As FileInfo() = di.GetFiles()
     
        lstCSV.DataSource = diar1
        lstCSV.DataBind()
     
    End Sub

    finally, i have the following in the ItemDataBOund event of the RadListView

    Private Sub lstCSV_ItemDataBound(sender As Object, e As RadListViewItemEventArgs) Handles lstCSV.ItemDataBound
     
         Dim lblFileName As New Label
         Dim lblHeaderName As New Label
         Dim tempHeaderName As String = String.Empty
     
         lblFileName.Text = TryCast(TryCast(e.Item, RadListViewDataItem).DataItem, FileSystemInfo).Name
     
     
         Dim s As String() = uploadCSV.AllowedFileExtensions
     
         For i As Integer = 0 To s.Length - 1
             If tempHeaderName = String.Empty Then
                 tempHeaderName = lblFileName.Text.Replace("." + s(i).ToString, "")
             Else
                 tempHeaderName = tempHeaderName.Replace("." + s(i).ToString, "")
             End If
         Next
     
     
     
         Dim lblBindName As New Label
         Dim lblBindHeader As New Label
     
         lblBindName = e.Item.FindControl("lblFileName")
         lblBindHeader = e.Item.FindControl("lblHeader")
     
         If Not IsNothing(lblBindName) Then lblBindName.Text = lblFileName.Text
         If Not IsNothing(lblBindHeader) Then lblBindHeader.Text = tempHeaderName
     
     
     End Sub

  2. Answer
    Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1800 posts

    Posted 12 Dec 2013 Link to this post

    Hi David,

    I have notice In your code snippet that you are trying to find the Label control within the group template from the RadListViewDataItem, but actually, the group RadListViewGroupItem is the parent item of the RadListViewDataItem, so you will have to use the following to get reference to that Label:
    Dim groupHeaderLabel As Label = TryCast((TryCast(e.Item, RadListViewDataItem).Parent).FindControl("lblHeader"), Label)

    For your convenience I have prepared a sample page, using your code as a reference, that works as expected on my end. Please take a look at the attached files and see if the results meet your requirements.


    Regards,
    Konstantin Dikov
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. David
    David avatar
    38 posts
    Member since:
    May 2013

    Posted 12 Dec 2013 Link to this post

    nice one!
    its more concise than my version, and it works :)
    i did actually get it to work, but it was in a more round-about way. i've shown my line (commented out), along with your better version.


    cheers

       Private Sub lstCSV_ItemDataBound(sender As Object, e As RadListViewItemEventArgs) Handles lstCSV.ItemDataBound
     
           bindListView(TryCast(sender, RadListView), e)
     
       End Sub
     
     
     
     Private Sub bindListView(ByRef myRadListView As RadListView, ByRef e As RadListViewItemEventArgs)
     
         Dim lblBindHeader As New Label
         lblBindHeader = TryCast((TryCast(e.Item, RadListViewDataItem).Parent).FindControl("lblHeader"), Label)
         'lblBindHeader = TryCast(TryCast(myRadListView.Items(Index), RadListViewDataItem).BindingContainer, RadListViewGroupItem).FindControl("lblHeader")
    End Sub
Back to Top