I am using radtree view in my application.
I have a scenario in that on one tab selection the tree should show checkboxes & on other tab It shouldn’t display the check boxes.
Which I am handling through the tree.CheckBoxes Property making it true or false I am not even binding the tree again.
But at that time trees checkboxes is losing its state.(i.e. whatever nodes I have selected become unselected when I come to previous tab)
Is there any way to maintain the state of checkboxes of tree?
Thanks in advance
5 Answers, 1 is accepted
Following is the code that I tried to achieve your scenario.
ASPX:
<
telerik:RadTreeView
ID
=
"tree"
runat
=
"server"
onnodeclick
=
"tree_NodeClick"
DataSourceID
=
"SqlDataSource1"
DataFieldID
=
"id"
DataFieldParentID
=
"parent"
DataTextField
=
"name"
DataValueField
=
"id"
onnodecheck
=
"tree_NodeCheck"
>
</
telerik:RadTreeView
>
C#:
private
Hashtable CustomersChecked
{
get
{
object
res = ViewState[
"_cc"
];
if
(res ==
null
)
{
res =
new
Hashtable();
ViewState[
"_cc"
] = res;
}
return
(Hashtable)res;
}
}
protected
void
tree_NodeClick(
object
sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
{
for
(
int
i=0;i<tree.GetAllNodes().Count;i++)
{
object
isChecked =
null
;
isChecked = CustomersChecked[tree.GetAllNodes()[i].Value];
if
(isChecked !=
null
)
{
tree.GetAllNodes()[i].Checked = (
bool
)isChecked ==
true
;
}
}
if
(tree.CheckBoxes ==
false
)
{
tree.CheckBoxes =
true
;
}
else
tree.CheckBoxes =
false
;
}
protected
void
tree_NodeCheck(
object
sender, RadTreeNodeEventArgs e)
{
Hashtable target = CustomersChecked;
if
(e.Node.Checked ==
true
)
{
target[e.Node.Value]=
true
;
}
else
target[e.Node.Value] =
true
;
}
Hope this helps.
Thanks,
Shinu.
Thanks for the quick reply.
But this code is not working can you give me a sample code project in which on clicking of the buttons the tree is switching with checkboxes state.
on click of one button the tree removes its check boxes & on click of other button the tree appear with its check boxes with its previous state.
Here is the sample code that I tried based on your scenario.
ASPX:
<
telerik:RadTreeView
ID
=
"tree"
runat
=
"server"
DataSourceID
=
"SqlDataSource1"
DataFieldID
=
"id"
DataFieldParentID
=
"parent"
DataTextField
=
"name"
DataValueField
=
"id"
OnNodeCheck
=
"tree_NodeCheck"
>
</
telerik:RadTreeView
>
<
asp:Button
ID
=
"addCheckBox"
runat
=
"server"
OnClick
=
"addCheckBox_Click"
/>
<
asp:Button
ID
=
"removeCheckBox"
runat
=
"server"
OnClick
=
"removeCheckBox_Click"
/>
C#:
private
Hashtable CustomersChecked
{
get
{
object
res = ViewState[
"_cc"
];
if
(res ==
null
)
{
res =
new
Hashtable();
ViewState[
"_cc"
] = res;
}
return
(Hashtable)res;
}
}
protected
void
tree_NodeCheck(
object
sender, RadTreeNodeEventArgs e)
{
Hashtable target = CustomersChecked;
if
(e.Node.Checked ==
true
)
{
target[e.Node.Value]=
true
;
}
else
target[e.Node.Value] =
true
;
}
protected
void
addCheckBox_Click(
object
sender, EventArgs e)
{
for
(
int
i = 0; i < tree.GetAllNodes().Count; i++)
{
object
isChecked =
null
;
isChecked = CustomersChecked[tree.GetAllNodes()[i].Value];
if
(isChecked !=
null
)
{
tree.GetAllNodes()[i].Checked = (
bool
)isChecked ==
true
;
}
}
if
(tree.CheckBoxes ==
false
)
{
tree.CheckBoxes =
true
;
}
}
protected
void
removeCheckBox_Click(
object
sender, EventArgs e)
{
for
(
int
i = 0; i < tree.GetAllNodes().Count; i++)
{
object
isChecked =
null
;
isChecked = CustomersChecked[tree.GetAllNodes()[i].Value];
if
(isChecked !=
null
)
{
tree.GetAllNodes()[i].Checked = (
bool
)isChecked ==
true
;
}
}
if
(tree.CheckBoxes ==
true
)
{
tree.CheckBoxes =
false
;
}
}
Hope this helps.
Thanks,
Shinu.
I have tried this code but it is not working properly.
I think it is working exactly opposite which is expected also tristate=true property of the tree is not mentioned.
Even all the tree nodes are checked on pageload
Another way to achieve such functionality is to save the checked state in separate field by using custom attributes and after a click of a button to associate each node with its check state.
Hope this will be helpful.
Plamen Zdravkov
the Telerik team