Treeview Checked Item = False when it is True

  • Thread starter Derek Pettinato
  • Start date
D

Derek Pettinato

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.
 
M

MSFT

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.)
 
D

Derek Pettinato

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.
 
M

MSFT

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.)
 
D

Derek Pettinato

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.
 
M

MSFT

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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top