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

Treeview checkboxes state

5 Answers 87 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Sampada
Top achievements
Rank 1
Sampada asked on 20 Jun 2012, 07:55 AM

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

Sort by
0
Shinu
Top achievements
Rank 2
answered on 20 Jun 2012, 11:57 AM
Hi Sampada,

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.
0
Sampada
Top achievements
Rank 1
answered on 21 Jun 2012, 09:26 AM

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.

0
Shinu
Top achievements
Rank 2
answered on 21 Jun 2012, 10:28 AM
Hi Sampada,

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.
0
Sampada
Top achievements
Rank 1
answered on 21 Jun 2012, 11:29 AM

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

0
Accepted
Plamen
Telerik team
answered on 25 Jun 2012, 06:54 AM
Hi Sampada,

 
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.

Greetings,
Plamen Zdravkov
the Telerik team
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 their blog feed now.
Tags
TreeView
Asked by
Sampada
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Sampada
Top achievements
Rank 1
Plamen
Telerik team
Share this question
or