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

access control in GroupTemplate

2 Answers 79 Views
ListView
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 09 Dec 2013, 02:49 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 12 Dec 2013, 09:44 AM
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.
0
David
Top achievements
Rank 1
answered on 12 Dec 2013, 10:21 AM
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
Tags
ListView
Asked by
David
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
David
Top achievements
Rank 1
Share this question
or