Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
I am trying to find out how to check the 'check' state of a node in a TreeView control using C# in an ASP.NET application. Basically, I have a TreeView control that shows checkboxes. The user makes a selection which I want to store in a session variable upon a 'submit' button click. I use a 'TreeView.Node.Checked' statement to verify whether the user selected that node. This, however, does not work because the selection is made at the client side and when it gets posted to the server, the 'TreeView.Node.Checked' statement, whether selected or not, will always return 'false' for any node. Does anyone have a solution for this?
Thanks,
Ralf
Posted
Updated 3-May-11 4:10am
v2
Comments
Hemant__Sharma 3-May-11 10:35am    
Provide some code to better understand the issue and fast solution. For example how have you added checkboxes to treeview control

In my .aspx file, I have:

XML
<td>
    <asp:TreeView ID="tvTaxon" runat="server" ShowCheckBoxes="All">
    </asp:TreeView>
</td>


The treeview control gets populated at initialization from a SQL Server table. That all works, what I'm not sure about is the issue w/ the postback. I understand that clicking on a checkbox will not raise a postback, but clicking on a button should. So, after selecting 'submit', should the server not get the updated Treeview and process it accordingly?
 
Share this answer
 
Comments
Hemant__Sharma 3-May-11 11:36am    
Please donot add code in seperate solutions every time you can edit your question or "Add comment" to a comment or click on Improve solution link.
Sure, here it is:

The submit button code:
C#
protected void btSubmit_Click(object sender, EventArgs e)
{
    TraverseTreeView(tvTaxon); //should return all nodes that were checked by user
}


The pertinent 'Traverse' code snippet:
C#
private void TraverseTreeView(TreeView tview)
{
    //Create a TreeNode to hold the Parent Node
    TreeNode temp = new TreeNode();
    string taxon;
    //Loop through the Parent Nodes
    for (int k = 0; k < tview.Nodes.Count; k++)
    {
        //Store the Parent Node in temp
        temp = tview.Nodes[k];
        //Display the Text of the Parent Node i.e. temp
        if (temp.Checked) //PROBLEM HERE! This will always evaluate to 
                          //false, no matter if user checks node or not
        {
            //store in session variable;
        }
    }
    //Now Loop through each of the child nodes in this parent node i.e.temp
    for (int i = 0; i < temp.ChildNodes.Count; i++)
        visitChildNodes(temp.ChildNodes[i]); //send every child to the function for further traversal
}
 
Share this answer
 
Comments
Hemant__Sharma 3-May-11 11:07am    
Thanks Ralf, please add the markup/code where you have added Checkboxes to the treeview
Check viewstate of your page is it false?

2nd are you binding your treeview everytime when page is loading or Init? ideally if viewstate is true of your page you shouldn't bind data to treeview every time. Bind it on first page load i.e.

if(!IsPostBack)
   tvTaxon.DataSource = DataSource...


FYI:
There are three basic phases of every server control happens every time:
Init
Load
Render

if there is any data control TreeView in your case then databind also happens. Most of the data control adds datasource to viewstate when first time you supply data to it, on subsequent postbacks TreeView extracts data from viewstate thats why we don't need to pass it again-n-again.

If you are sure you are not binding your TreeView everytime time please add the code where you are binding your treeview to the SQL datasource. the code should include the event name in which you are assigning the source for example Page_load, the code should also show if there is any if statment prior to assigning datasource.

ideally you should add all code i.e. markup of aspx + code where you are binding data + code where you are evaluating check statements like you added in the first comment.

Thanks,
Hemant
 
Share this answer
 
Comments
RalfPeter 3-May-11 11:57am    
Let me work on this. Thx for you time and effort,

Ralf

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900