Rodrigo wrote:1. When I said "pre-populate" I meant to be able to read from my Data Base what checkboxes were checked before by the user, and when binding the tree view, to be able to check those check boxes automatically. When I bind the tree view from a DataTable, is there a way to set the value property of the node?
This is exactly what data binding does. Data binding allows you to populate the TreeView and then check the checkboxes automatically based on your data source. Please take a look of the documentation first.
Rodrigo wrote:2. What I need is to iterate through all the treeview nodes and see which checked boxes are checked, once I know this, I want to retrieve their values in order to be able to change a flag in my Data Base. All this has to be done when the user presses an asp button, and no every time he checks the checkbox.
ItemCheckStateChanged is the correct solution. The event is NOT fired when you check the checkbox. It is fired when your page posts back (when user presses a button). What this event does is to help you to identify which node has been changed. Consider the following scenario:
1. You have 10 records, with 6 of them checked;
2. You populate the TreeView from your DB, 6 tree nodes are automatically checked based on data in your db;
3. User checked another 2 nodes. So now 8 nodes are checked;
4. User clicks a button to submit the changes;
Now you have two options:
1. Iterate through all 10 nodes (you can do this with recursive code easily) and check each nodes' Checked property and update your DB 10 times;
-- OR --
2. Handle ItemCheckStateChanged event. This event is fired
for each treenode whose check state has been changed. Because in this case only 2 nodes has changed, so this event will be fired two times. As a result, you will update your DB only twice;
Obviously option 2 is much better because it only updates what has been changed.
Hope this clears up.
Thanks