The tree view's data source can be changed by the user threw a dropdown. The Treeview's datasource and DataBinding is done in the OnPreRender event so that the newly selected value in the dropdown are cought (so that we don't get PostBack problem returning the earlier selected value).
The TreeView control works fine the first time the page is loaded, but when the user selects a new value ( = Datasource ) in the drop down, trigging a Postback I get this error :
Sys.InvalidOperationException: A control is already associated with the element.
http://localhost/ScriptResource.axd?d=I_sV7_5aqsHUuZz1dlHW-RqZsi4HVpivLZbDA9G0_dLCsiXBM93DwJPASsxTuNt_QVQWDeUc-OuHZjNgQ_3i5s507ruArCU5OS77T0Ea8jM1&t=633401439743849103
Line 7278
if (typeof(element.control) != 'undefined') throw Error.invalidOperation(Sys.Res.controlAlreadyDefined);
This worked fine with the old version of the TreeView. It seems that there are some conflicts when the page is PostBack and the ScriptManager doesn't clean the control cache or something. It should be Dissposed by the AJAX.NET framework.
I've tried clearing the ScriptManagers control collection in the OnInit and PreRender events but it didn't help.
The error dissapears when I set EnableViewState="false" for the TreeView but then I loose all references so I can't load the child nodes and so on.
The code for loading the first nodes are
Protected
Overrides Sub
OnPreRender(ByVal
e As
LoadNodes()
MyBase.OnPreRender(e)
End
Sub
Renders the nodes at the top level
Private
Sub
LoadNodes()
RadTreeView.Nodes.Clear()
RadTreeView.ContextMenus.Add(GetContextMenues())
RadTreeView.DragAndDrop =
True
For Each
dr As DataRow In
MigEl.DAL.Dimention.GetSingleTreeLevel(SelectedDimention(), 1, Nothing, GetSelectHierarcy()).Rows
Dim
node As RadTreeNode = New
RadTreeNode
node.Value = dr("Id").ToString()
node.Text = dr("Description").ToString()
node.Attributes.Add("RkId", dr("RkId").ToString())
node.ExpandMode =
TreeNodeExpandMode.ServerSideCallBack
node.ContextMenuID = "MemberMenu"
RadTreeView.Nodes.Add(node)
Next
RadTreeView.DataBind()
End
Sub
'''
<summary>
''' Handles the NodeExpand event of the
RadTreeView control.
'''
</summary>
''' <param name="o">The source of the event.</param>
''' <param name="e">The <see
cref="Telerik.WebControls.RadTreeNodeEventArgs" /> instance containing the event
data.</param>
Protected Sub RadTreeView_NodeExpand(ByVal o As Object, ByVal e As RadTreeNodeEventArgs) Handles RadTreeView.NodeExpand
Dim level As
Integer = If(e.Node Is
Nothing, 0, e.Node.Level)
For
Each dr As
DataRow In
MigEl.DAL.Dimention.GetSingleTreeLevel(SelectedDimention(), level + 2,
e.Node.Value, GetSelectHierarcy()).Rows
Dim
node As RadTreeNode = New
RadTreeNode
node.Value = dr("Id").ToString()
node.Text = dr("Description").ToString()
node.Attributes.Add("RkId", dr("RkId").ToString())
node.ExpandMode =
TreeNodeExpandMode.ServerSideCallBack
node.ContextMenuID = "MemberMenu"
e.Node.Nodes.Add(node)
Next
End Sub