如何确定TreeView.SelectedNode是父节点还是子节点

时间:2012-07-10 09:44:57

标签: c# asp.net events triggers treeview

enter image description here我的ASP.NET Web应用程序中有一个TreeView控件,我在选择的节点更改时出现问题,我点击树视图中的某个节点但是事件没有触发,我有那些没有执行的指令,我也用调试器检查过。

<asp:TreeView ID="ArboreSarcini" runat="server" ImageSet="News" 
onselectednodechanged="ArboreSarcini_SelectedNodeChanged" NodeIndent="10" 
style="z-index: 1; left: 1px; top: 27px; position: absolute; height: 308px; width: 446px">
<HoverNodeStyle Font-Underline="True" BackColor="#99CCCC" Font-Size="12pt" />
<LeafNodeStyle ImageUrl="~/Poze/leaf.png" NodeSpacing="2px" />
<LevelStyles>
<asp:TreeNodeStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" 
Font-Names="Leelawadee" Font-Size="Medium" Font-Underline="False" 
ImageUrl="~/Poze/Root.png" />
<asp:TreeNodeStyle BorderColor="#003300" BorderStyle="Solid" BorderWidth="1px" 
Font-Names="Leelawadee" Font-Underline="False" ImageUrl="~/Poze/node.png" 
Width="400px" />
<asp:TreeNodeStyle BorderColor="#006600" BorderStyle="Solid" BorderWidth="1px" 
Font-Names="Leelawadee" Font-Underline="False" Height="10px" 
ImageUrl="~/Poze/leaf.png" Width="390px" />
</LevelStyles>
<NodeStyle Font-Names="Leelawadee" Font-Size="10pt" ForeColor="Black" 
HorizontalPadding="5px" NodeSpacing="2px" VerticalPadding="0px" 
ImageUrl="~/Poze/node.png" Width="0px" />
<ParentNodeStyle Font-Bold="False" Width="500px" />
<RootNodeStyle ImageUrl="~/Poze/Root.png" />
<SelectedNodeStyle Font-Underline="False" BackColor="#009148" BorderWidth="2px" 
Font-Bold="False" Font-Italic="True" Font-Size="12pt" />
</asp:TreeView>
protected void Button1_Click(object sender, EventArgs e)
{
ArboreSarcini.Nodes.Clear();
populeaza_sarcini();

string sqlstring1 = "Select * from activitati";
System.Data.SqlClient.SqlConnection con1 = new System.Data.SqlClient.SqlConnection(
            "Data Source=BOGDAN-PC\\BOGDAN;Initial Catalog=ePlanning;Integrated Security=SSPI;Connect Timeout=10;TrustServerCertificate=True ");
System.Data.SqlClient.SqlCommand comm1 = new System.Data.SqlClient.SqlCommand(sqlstring1, con1);
System.Data.SqlClient.SqlDataReader reader1;
con1.Open();
reader1 = comm1.ExecuteReader();

while (reader1.Read())
{
foreach (Sarcina s in listaSarcini)
{
    if ((int)reader1["id_sarcina"] == s.Id_sarcina)
    {
        s.ListaActivitati.Add(new Activitate(Convert.ToInt32(reader1["id_activitate"]), reader1["descriere"].ToString()));
    }
}
}

TreeNode tatal = new TreeNode();
tatal.Value = DropListProiecte.SelectedItem.ToString();
//    ArboreSarcini.Nodes.Add(tatal);


TreeNode parentNode = new TreeNode();
foreach (Sarcina sarc in listaSarcini)
{
parentNode = new TreeNode( sarc.Id_sarcina.ToString() + ". " + sarc.Descriere.ToString());


foreach (Activitate act in sarc.ListaActivitati)
{
    TreeNode copil = new TreeNode(act.Id_activitate.ToString()+". "+act.Descriere.ToString() );
    parentNode.ChildNodes.Add(copil);
}
tatal.ChildNodes.Add(parentNode);
//parentNode.Collapse();


}
ArboreSarcini.Nodes.Add(tatal);
con.Close();
ArboreSarcini.ExpandAll();
}


    protected void ArboreSarcini_SelectedNodeChanged(object sender, EventArgs e)
{

    if (ArboreSarcini.SelectedNode.ImageUrl == "~/Poze/node.png")
    {

        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(
                      "Data Source=BOGDAN-PC\\BOGDAN;Initial Catalog=ePlanning;Integrated Security=SSPI;Connect Timeout=10;TrustServerCertificate=True ");
        //  string de_splituit = ArboreSarcini.SelectedNode.Text;
        string[] id_sarcina = ArboreSarcini.SelectedNode.Text.Split('.');


        string sqlstring = "Select * from sarcini where id_sarcina=" + id_sarcina[0] + ";";




        System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand(sqlstring, con);
        System.Data.SqlClient.SqlDataReader reader;

        con.Open();
        reader = comm.ExecuteReader();
        while (reader.Read())
        {
            tbIDSarcina.Text = reader["id_sarcina"].ToString();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

默认情况下,TreeView控件处理展开折叠功能   在客户端上,除非浏览器不支持客户端脚本或   EnableClientScript属性设置为false。如果   PopulateNodesFromClient属性设置为true和浏览器   支持客户端脚本,然后TreeView控件检索数据   从服务器而不发布整个页面。

     

当TreeView控件处于选择模式时,每次都是用户   单击一个节点,发生一个回发到服务器和   引发了SelectedNodeChanged事件。

     

通常,您应该在TreeView控件时处理回发事件   处于选择模式或节点正在动态填充。   这是因为PopulateOnDemand或PopulateNodesFromClient   property设置为true。

请亲自验证您是否设置了所有必需的属性。分享您的HTML可能会有帮助。 阅读更多关于asp.net treeview here的信息 TreeNode.Nodes属性。 获取分配给当前树节点的TreeNode对象的集合。

http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.nodes.aspx