Treeview Checked Item = False when it is True

Discussion in 'ASP .Net Web Controls' started by Derek Pettinato, Nov 5, 2003.

  1. Hi,

    I have a treeview on a webform bound to an external XML
    file with checkboxes.

    I have some javascript on the page that basically fires
    when the checkboxes are checked to uncheck anything that
    may be checked in the same node thus allowing only one
    node to be checked at one time.

    On this form I have a submit button that run some vb
    codebehind. All I need this codebehind to do is to go
    through the entire treeview and grab all of the items
    that are checked. I figured this would be pretty easy
    with the .checked property. Not the case as when
    debugging the codebehind, the checked value of the node I
    checked is always false.

    Also, I cannot have the javascript do this as I need to
    run back to the server to populate dynamically generated
    tables with the data that is checked on the treeview.

    My code is below...

    '**********button clicked***************
    Public Sub TreeSelect(ByVal sender As System.Object,
    ByVal e As
    Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs)

    Dim dimArray() As String
    ReDim dimArray(TreeView1.Nodes.Count)

    For i = 0 To TreeView1.Nodes.Count - 1
    dimArray(i) = ProcessNodes(TreeView1.Nodes)
    Next
    '**************************************

    '********function*************************
    Public Function ProcessNodes(ByVal ParentNode As
    TreeNodeCollection) As String

    Dim childNode As TreeNode

    For Each childNode In ParentNode
    If childNode.Checked = True Then
    ProcessNodes = childNode.Text
    End If
    If childNode.Nodes.Count > 0 Then
    If childNode.Checked = False Then
    ProcessNodes(childNode.Nodes)
    Else 'checked!
    ProcessNodes = childNode.Text
    End If
    End If
    Next childNode
    '*********************************

    Thank you in advance,
    -Derek.
     
    Derek Pettinato, Nov 5, 2003
    #1
    1. Advertising

  2. Derek Pettinato

    MSFT Guest

    Hi Derek,

    After invest on your code, I think there is a little mistake in it and it
    should be:

    If childNode.Checked = False Then
    ProcessNodes = ProcessNodes(childNode.Nodes)
    Else 'checked!
    ProcessNodes = childNode.Text
    End If

    Your original code is :

    If childNode.Checked = False Then
    ProcessNodes(childNode.Nodes)
    Else 'checked!
    ProcessNodes = childNode.Text
    End If

    The result can't be return from under layer call

    Hope this help,

    Luke
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    MSFT, Nov 6, 2003
    #2
    1. Advertising

  3. Hi,

    Thank you for your quick response but that did not seem
    to fix the problem.

    For some reason the value of the checkbox never is true.
    I have stepped throough the code watching the values and
    when they are checked, they remain False.

    I am not sure if the way I have the treeview control
    setup has something to do with its code-behind behavior?
    The treeview's AutoPostBack property is set to False. I
    do not want the treeview to go back to the server until
    the user clicks on the Submit button. I use the following
    javascript to check/uncheck nodes so there is only one
    checked at a time...

    '****Page Load Code-Behind********
    TreeView1.Attributes.Add("oncheck", "DoCheck(this);")
    '*********************************

    '****javascript*******************
    // Function that allows only one value per Parent Node to
    be checked at one time.
    function DoCheck(oTree)
    {

    // Get the node that fired the event...
    var oNodeClicked = oTree.getTreeNode
    (oTree.clickedNodeIndex);

    // Split the clickedNodeIndex to determine the Parent
    var strSplit = (oTree.clickedNodeIndex).split(".");

    // Set the Parent to 'unchecked' since we are going to
    traverse and 'uncheck' all children
    var oParentNode = oTree.getTreeNode(strSplit[0]);
    oParentNode.setAttribute("checked", false);

    // Check if the node is checked or unchecked
    var bChecked = (oParentNode.getAttribute("checked")
    ==true) ? false : true;

    // Get the nodes childnodes...
    var oColl = oParentNode.getChildren();
    //var oColl = oRoot.getChildren();

    // Call the function that traverses the nodes
    TraverseChildren(oColl, bChecked);

    // Set the originally clicked node to checked
    oNodeClicked.setAttribute("checked", true);
    }

    // Recursive function that traverses through the tree and
    checks/unchecks the nodes
    function TraverseChildren(oColl, bChecked){
    var oChild;
    for (var i=0;i<oColl.length;i++){
    oChild = oColl;

    // Check/Uncheck the current node by accessing the
    setAttribute
    (bChecked) ? oChild.setAttribute("checked", false) :
    oChild.setAttribute("checked", true);

    // If you only want to select 1st level children
    // you'll have to comment this line out...
    TraverseChildren(oChild.getChildren(), bChecked);
    }
    }
    '*****************************************

    ....after checking one or many nodes and clicking on the
    aspx button 'Select' it seems like the treeview control
    gets 'reset' on its way back to the server before the
    code-behind runs.

    Do you think I may have to resort to hidden fields
    populated by the DoCheck javascript function to capture
    the values of the checked nodes and then on the Select,
    read those hidden fields since the treeview isn't
    returning its new values to the server?

    Thank you again in advance,
    -Derek.

    >-----Original Message-----
    >Hi Derek,
    >
    >After invest on your code, I think there is a little

    mistake in it and it
    >should be:
    >
    > If childNode.Checked = False Then
    > ProcessNodes = ProcessNodes

    (childNode.Nodes)
    > Else 'checked!
    > ProcessNodes = childNode.Text
    > End If
    >
    >Your original code is :
    >
    > If childNode.Checked = False Then
    > ProcessNodes(childNode.Nodes)
    > Else 'checked!
    > ProcessNodes = childNode.Text
    > End If
    >
    >The result can't be return from under layer call
    >
    >Hope this help,
    >
    >Luke
    >Microsoft Online Support
    >
    >Get Secure! www.microsoft.com/security
    >(This posting is provided "AS IS", with no warranties,

    and confers no
    >rights.)
    >
    >
    >.
    >
     
    Derek Pettinato, Nov 6, 2003
    #3
  4. Derek Pettinato

    MSFT Guest

    Hi Derek,

    Is the problem related to DoCheck method? If you remove the line:

    TreeView1.Attributes.Add("oncheck", "DoCheck(this);")

    Will you get the correct values in code behind?

    Luke
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    MSFT, Nov 7, 2003
    #4
  5. Hi,

    Yes, when I remove the line TreeView1.Attributes.Add
    ("oncheck", "DoCheck(this);") the code behind has the
    correct checked values...

    So, is there a way to still allow the addition of the
    attribute at runtime so the javascript runs when the user
    clicks on a checkbox? Or, is there another way for me to
    obtain the same results I get on the user click and still
    have the code behind have the correct values?

    Thank you,
    -Derek.
    >-----Original Message-----
    >Hi Derek,
    >
    >Is the problem related to DoCheck method? If you remove

    the line:
    >
    >TreeView1.Attributes.Add("oncheck", "DoCheck(this);")
    >
    >Will you get the correct values in code behind?
    >
    >Luke
    >Microsoft Online Support
    >
    >Get Secure! www.microsoft.com/security
    >(This posting is provided "AS IS", with no warranties,

    and confers no
    >rights.)
    >
    >.
    >
     
    Derek Pettinato, Nov 10, 2003
    #5
  6. Derek Pettinato

    MSFT Guest

    Hi Derek,

    Based on my test, this problem would occur when we add

    TreeView1.Attributes.Add("oncheck", "DoCheck(this);")

    in FormLoad. And it doesn't depend on what code we have in method DoCheck();

    It seems the only work around is adding a hidden input in the form and set
    selected node's value to it in DoCheck()

    Luke
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    MSFT, Nov 11, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yurps
    Replies:
    1
    Views:
    6,897
    rajeshthangarasu1983
    Feb 29, 2008
  2. Siemel Naran

    Does true ^ true return false?

    Siemel Naran, Jun 17, 2004, in forum: C++
    Replies:
    19
    Views:
    669
    Chris Theis
    Jun 18, 2004
  3. Pierre Quentel

    "0 in [True,False]" returns True

    Pierre Quentel, Dec 12, 2005, in forum: Python
    Replies:
    59
    Views:
    1,035
    Grant Edwards
    Dec 16, 2005
  4. André
    Replies:
    3
    Views:
    1,596
  5. bdb112
    Replies:
    45
    Views:
    1,348
    jazbees
    Apr 29, 2009
Loading...

Share This Page