Treeview checkboxes state

6 posts, 1 answers
  1. Sampada
    Sampada avatar
    44 posts
    Member since:
    Feb 2012

    Posted 20 Jun 2012 Link to this post

    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

                                   

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Jun 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Sampada
    Sampada avatar
    44 posts
    Member since:
    Feb 2012

    Posted 21 Jun 2012 Link to this post

    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.

  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Jun 2012 Link to this post

    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.
  6. Sampada
    Sampada avatar
    44 posts
    Member since:
    Feb 2012

    Posted 21 Jun 2012 Link to this post

    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

  7. Answer
    Plamen
    Admin
    Plamen avatar
    2733 posts

    Posted 25 Jun 2012 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017