Determine change in state of checkbox

8 posts, 0 answers
  1. Polaris431
    Polaris431 avatar
    70 posts
    Member since:
    May 2007

    Posted 28 Dec 2009 Link to this post

    What is the simplist way to determine whether the checkbox for a node has changed its state. Currently I use Javascript code to store changes in a variable and send the contents of that variable back to the server. I suppose what I am looking for is something like a "CheckboxChanged" property. It really shouldn't have to be necessary for me to write additional code to keep track of these changes. It should be built into the Treeview control. In typical scenarios, when a user checks or unchecks something, this will often result in having to add or delete something on the server. Without recording the state, I would have to first get what already exists on the server and compare it to the current treeview state and either add or remove stuff.
  2. Yana
    Admin
    Yana avatar
    5015 posts

    Posted 28 Dec 2009 Link to this post

    Hello,

    I suggest you use OnClientNodeChecked client-side event or NodeCheck server-side event of RadTreeView.

    Best regards,
    Yana
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Polaris431
    Polaris431 avatar
    70 posts
    Member since:
    May 2007

    Posted 28 Dec 2009 Link to this post

    Hi Yana,

    I already am using those events. That's not what I was asking. I already indicated that I am writing Javascript code to handle the state between client and server. I was looking for a built-in way from the control to handle this for me.

    Johann
  4. Yana
    Admin
    Yana avatar
    5015 posts

    Posted 28 Dec 2009 Link to this post

    Hello Johann,

    I'm sorry I didn't understand the question right.

    You can use ClientChanges property of RadTreeView to access the client changes after postback.

    Greetings,
    Yana
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Polaris431
    Polaris431 avatar
    70 posts
    Member since:
    May 2007

    Posted 28 Dec 2009 Link to this post

    Hi Yana,

    Seems like we're getting close. However, it appears that using the ClientChanges object is only available on the server-side. Does this not mean that I must persist the entire treeview? At the moment, I have the treeview's EnableViewState set to "false" for performance reasons. I then use the AjaxManager to send changes in the treeview back to the server. Therefore, I don't believe the ClientChanges will be of any value. I just upgraded my RadControls from Q1 to the Q3 version. Is there anything in the Q3 version that allows me to persist the state of the treeview and then use ClientChanges?

    Thanks
    Johann
  6. Polaris431
    Polaris431 avatar
    70 posts
    Member since:
    May 2007

    Posted 28 Dec 2009 Link to this post

    Hi Yana,

    I even tried setting the EnableViewState of the treeview to "true". I then changed the checkbox state on a node and did a postback. The ClientChanges object contained no changes. It does not appear that changing the "Checked" property on the client-side has any effect on ClientChanges.

    Johann
  7. Polaris431
    Polaris431 avatar
    70 posts
    Member since:
    May 2007

    Posted 28 Dec 2009 Link to this post

    My mistake. If EnableViewState is set to true, it will detect changes to any attributes on the client side. I was forgetting to use trackChanges and commitChanges. However, it will not detect changes to properties. Changing the state of the checkbox results in the "Checked" property being changed but ClientChanges does not indicate this.
  8. Dimitar Milushev
    Admin
    Dimitar Milushev avatar
    555 posts

    Posted 29 Dec 2009 Link to this post

    Hello,

    You are right, the ClientChanges does not contain this info. Please excuse us for misleading you.

    The simplest way I think would be to use the Node's Attributes collection and handle the OnClientNodeChecked event. The in the nodeChecked handler you can do this:

    function nodeChecked(sender, args)
    {
        var node = args.get_node();
        var newState = node.get_attributes().getAttribute("newState");
         
        treeView.trackChanges();
         
        if(!newState) {
            // Original state hasn't be changed before so we record the new state
            if (node.get_checked())
                node.get_attributes().setAttribute("newState", "checked");
            else
                node.get_attributes().setAttribute("newState", "unchecked");
        }
        else {
            // We are reverting to our original state, clear the attribute
            node.get_attributes().setAttribute("newState", "");
        }
         
        treeView.commitChanges();
    }

    When you postback, on the server-side the "newState" Attribute will be set to checked/unchecked only if the state of the checkbox is different from the original. No matter how many times you click the checkbox, if you leave it in the original state when you postback, the Attribute will be an empty string.

    I hope this helps.

    Kind regards,
    Dimitar Milushev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top