Asp.net Treeview ChildNodes。 [英] Asp.net Treeview ChildNodes.
问题描述
这里我有这个代码,我想删除数据库中的所有条目,这些条目在树视图复选框中未选中。我只能到达父节点。我的代码直到子节点才能到达。请帮忙。
p.s.我不想删除节点,我只想找出在button_click事件中取消选中哪些节点并删除数据库中这些节点的值。
感谢
foreach(TreeNode tn in this.TreeView1.Nodes)
{
int strTreeValue = Convert.ToInt32(tn.Value);
if(tn.Checked == false)
{
SqlCommand com = new SqlCommand(Delete from Role_Menu Where Menu_id =+ strTreeValue,con) ;
com.ExecuteNonQuery();
}
if(tn.ChildNodes.Count> 0)
{
foreach(TreeView1.Nodes中的TreeNode Ctn)
{
SqlCommand com = new SqlCommand(从Role_Menu中删除,其中Menu_id = + strTreeValue,con) ;
com.ExecuteNonQuery();
}
}
}
here i have this code where i want to delete all the entries from database which are unchecked in the treeview checkbox. i am able to reach only till the parent nodes.my code doesnt reach till childnodes. kindly help.
p.s. i dont want to delete the nodes, i just want to find out which nodes are unchecked on button_click event and delete the values of those nodes in the database.
thanks
foreach (TreeNode tn in this.TreeView1.Nodes)
{
int strTreeValue = Convert.ToInt32(tn.Value);
if (tn.Checked == false)
{
SqlCommand com = new SqlCommand("Delete From Role_Menu Where Menu_id=" + strTreeValue, con);
com.ExecuteNonQuery();
}
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode Ctn in TreeView1.Nodes)
{
SqlCommand com = new SqlCommand("Delete From Role_Menu Where Menu_id=" + strTreeValue, con);
com.ExecuteNonQuery();
}
}
}
推荐答案
我认为你犯了一个简单的错误。您需要找到子节点,因此您的循环必须通过tn.ChildNodes。见下面的代码块......
你的代码:
I think you make a simple mistake. You need to find Child Nodes so your loop must be go through the "tn.ChildNodes". see below code block...
Your code:
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode Ctn in TreeView1.Nodes)
{
SqlCommand com = new SqlCommand("Delete From Role_Menu Where Menu_id=" + strTreeValue, con);
com.ExecuteNonQuery();
}
}
更改代码:
Change Code:
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode Ctn in tn.ChildNodes)
{
SqlCommand com = new SqlCommand("Delete From Role_Menu Where Menu_id=" + strTreeValue, con);
com.ExecuteNonQuery();
}
}
试试这个可能对你有帮助。
Try this maybe it''s help you.
>
您想删除未经检查的节点。是吗?
You want to delete unchecked nodes . is it?
我想删除所有未经检查的节点的数据库条目,包括父节点和子节点。
i want to delete the database entries of all unchecked nodes including parent and child nodes.
这篇关于Asp.net Treeview ChildNodes。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!