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

[Solved] Reading values from Templated Controls

3 Answers 181 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 16 Feb 2010, 08:50 AM
Hi,

I have a treeview that I'm using to provide a set of controls on each node.  There is the normal node text, a textbox and a checkbox.  I've got it all working except for reading the data back.  My routine can't use Node.FindControl to find any of the controls that I've created.  Some relevant snippets of code are shown below. 

I've read that I need to reallocate the template to the nodes on the ajax callback.  How do I go about this? (current update code at the bottom).

Regards,

Jon

'CODE FOR GENERATING THE TREE  
 
            uxRadTreeView.DataTextField = "LocationCombo" 
            uxRadTreeView.DataFieldID = "AuditEntryID" 
            uxRadTreeView.DataValueField = "AuditEntryID" 
            uxRadTreeView.DataSource = groups  
            uxRadTreeView.DataBind()  
            Dim template As NodeTemplate = New NodeTemplate()  
            template._InEditMode = _InEditMode  
            For Each node As RadTreeNode In uxRadTreeView.GetAllNodes()  
                template.InstantiateIn(node)  
                node.Expanded = True 
                node.DataBind()  
            Next 
 
' TEMPLATE CODE  
        Class NodeTemplate  
            Implements ITemplate  
            Public _InEditMode As Boolean 
 
            Public Sub New()  
            End Sub 
 
            Public Sub InstantiateIn(ByVal container As Control) Implements System.Web.UI.ITemplate.InstantiateIn  
                Dim uxLocationCombo As Label = New Label()  
                uxLocationCombo.ID = "uxLocationCombo" 
                uxLocationCombo.Width = 300  
                AddHandler uxLocationCombo.DataBinding, AddressOf nodeText_DataBinding  
                container.Controls.Add(uxLocationCombo)  
 
                If _InEditMode Then 
                    Dim uxScore As RadNumericTextBox = New RadNumericTextBox()  
                    uxScore.ID = "uxScore" 
                    uxScore.Enabled = True 
                    uxScore.Width = 60  
                    uxScore.MinValue = 1  
                    uxScore.MaxValue = 5  
                    uxScore.ShowSpinButtons = True 
                    uxScore.NumberFormat.DecimalDigits = 0  
                    AddHandler uxScore.DataBinding, AddressOf uxScore_DataBinding  
                    container.Controls.Add(uxScore)  
                Else 
                    Dim uxScore As Label = New Label()  
                    uxScore.ID = "uxScore" 
                    uxScore.Enabled = True 
                    uxScore.Width = 60  
                    AddHandler uxScore.DataBinding, AddressOf uxScoreLabel_DataBinding  
                    container.Controls.Add(uxScore)  
                End If 
 
 
                Dim uxNotScored As CheckBox = New CheckBox()  
                uxNotScored.ID = "uxNotScored" 
                uxNotScored.Enabled = _InEditMode  
                uxNotScored.Width = 60  
                AddHandler uxNotScored.DataBinding, AddressOf uxNotScoredCheckbox_DataBinding  
                container.Controls.Add(uxNotScored)  
            End Sub 
 
            Private Sub uxScoreLabel_DataBinding(ByVal sender As ObjectByVal e As EventArgs)  
                Try 
                    Dim target As Label = DirectCast(sender, Label)  
                    Dim node As RadTreeNode = DirectCast(target.BindingContainer, RadTreeNode)  
                    target.Text = node.Attributes("Score").ToString()  
                    'target.Attributes("onClick") = "onNodeClick(" & node.Attributes("DocumentTypeID").ToString() & ");"  
                Catch ex As Exception  
                End Try 
            End Sub 
 
            Private Sub uxScore_DataBinding(ByVal sender As ObjectByVal e As EventArgs)  
                Try 
                    Dim target As RadNumericTextBox = DirectCast(sender, RadNumericTextBox)  
                    Dim node As RadTreeNode = DirectCast(target.BindingContainer, RadTreeNode)  
                    target.Text = node.Attributes("Score").ToString()  
                    'target.Attributes("onClick") = "onNodeClick(" & node.Attributes("DocumentTypeID").ToString() & ");"  
                Catch ex As Exception  
                End Try 
            End Sub 
 
            Private Sub uxNotScoredCheckbox_DataBinding(ByVal sender As ObjectByVal e As EventArgs)  
                Try 
                    Dim target As CheckBox = DirectCast(sender, CheckBox)  
                    Dim node As RadTreeNode = DirectCast(target.BindingContainer, RadTreeNode)  
                    target.Checked = CBool(node.Attributes("NotScored").ToString())  
                    'target.Attributes("onClick") = "onNodeClick(" & node.Attributes("AuditEntryID").ToString() & ");"  
                Catch ex As Exception  
                End Try 
            End Sub 
 
            Private Sub nodeText_DataBinding(ByVal sender As ObjectByVal e As EventArgs)  
                Try 
                    Dim target As Label = DirectCast(sender, Label)  
                    Dim node As RadTreeNode = DirectCast(target.BindingContainer, RadTreeNode)  
                    target.Text = node.Attributes("LocationCombo").ToString()  
                    'target.Attributes("onClick") = "onNodeClick(" & node.Attributes("DocumentTypeID").ToString() & ");"  
                Catch ex As Exception  
                End Try 
            End Sub 
        End Class 
 
 
 
' CODE IN THE UPDATE BUTTON CALLBACK  
 
                For Each node As RadTreeNode In uxRadTreeView.Nodes  
 
                    i += 1  
                    idArray(i) = node.Value  
                    scoreArray(i) = DirectCast(node.TemplateControl.FindControl("uxScore"), RadNumericTextBox).Value.ToString()  
                    If DirectCast(node.TemplateControl.FindControl("uxNotScored"), CheckBox).Checked Then 
                        noScoreArray(i) = "1" 
                    Else 
                        noScoreArray(i) = "0" 
                    End If 
 
                Next 

3 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 19 Feb 2010, 09:29 AM
Hello Jon,

I've modified your code in order to demonstrate how to get the controls from the template. Please download the attached file and give it a try.

Regards,
Yana
the Telerik team

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 Public Issue Tracking system and vote to affect the priority of the items.
0
Jon
Top achievements
Rank 1
answered on 19 Feb 2010, 09:34 AM
Hi Yana,

Many thanks for that - I'll give this a go next week. 

For the time being I'd done it via some client side JS but it was a messy solution as it required some hidden fields to store the values.

Regards,

Jon
0
Yana
Telerik team
answered on 19 Feb 2010, 09:36 AM
Hi Jon,

If you have any difficulties or questions, do not hesitate to contact us again.

Regards,
Yana
the Telerik team

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 Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
Jon
Top achievements
Rank 1
Answers by
Yana
Telerik team
Jon
Top achievements
Rank 1
Share this question
or