How to check chekbox of correspoding node in Treeview control from codebehind?
I want to know how Can I check the checkbox of a asp.net tree view node ( it can be either parent or child) based on matching a condition from the code behind?First Im getting all the nodes from the database. After that I want to check the checkbox corresponding to the ID 开发者_运维知识库which that user is a part of.
I hope i'm able to explain my requirement clearly.
DataSet dt = objUserClient.GetAllUserClientGroupDetails(UserId);
foreach (TreeNode parent in TreeView1.Nodes)
{
foreach (TreeNode child in parent.ChildNodes)
{
for (int j = 0; j < dt.Tables[0].Rows.Count; j++)
{
if (child.Value.Trim() == dt.Tables[0].Rows[j]["ClientId"].ToString().Trim())
{
child.Checked = true;
parent.Checked = true;
break;
}
}
}
}
In the Page_Load event, put the following code:
TreeNode node = myTreeView.nodes[1];
nodes.Checked = true;
This will check the checkbox in the second node. There are other ways to find the node such as myTreeView.FindNode(path) or by enumerating through the nodes with a foreach loop. I can provide more info if you specify how you are identifying which notes need to be checked.
//Consider the below dataset holds data
DataSet dsItemsFind = new DataSet();
//Use Looping to browse through the Treeview and DataTable
for (int i = 0; i < Treeview1.Nodes.Count; i++)
{
for (int j = 0; j < dsItemsFind.Tables[0].Rows.Count; j++)
{
if (Treeview1.Nodes[i].Value.ToString() == dsItemsFind.Tables[0].Rows[j]["ColumnName"].ToString())
{
//If ur Treeview Node value is = the Column value your looking for
//Then the Below line will get called
Treeview1.Nodes[i].Checked=true;
}
}
}
精彩评论