【问题标题】:How to determine if a TreeView.SelectedNode is parent or child node如何确定 TreeView.SelectedNode 是父节点还是子节点
【发布时间】:2012-07-10 09:44:57
【问题描述】:

我在我的 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();
        }
    }
}

【问题讨论】:

  • 你如何填充你的树?也许您在每次回发时都这样做。
  • 你有什么代码可以给我们看吗?
  • 是的,如果有点搞砸了抱歉
  • 如果放断点是否命中ArboreSarcini_SelectedNodeChanged事件?
  • 是的,谢谢,我已经尝试在方法中使用较低的断点,看起来它触发了事件,我的 if (ArboreSarcini.SelectedNode.ImageUrl == "~/Poze/node.png" )阻止执行以下代码,我的错误,对不起,谢谢,但我有下一个问题,我如何确定我是单击父节点还是子节点(我有父节点没有的情况)孩子)

标签: c# asp.net events triggers treeview


【解决方案1】:

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

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

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

因此,请确认您设置了所有必需的属性。共享您的 HTML 可能会有所帮助。 阅读更多关于 asp.net 树视图 here TreeNode.Nodes 属性。 获取分配给当前树节点的 TreeNode 对象的集合。

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

【讨论】:

  • 我想我发现了问题所在,触发了事件,但是我的其他指令阻止了以下代码,并且我在代码中的断点太低进行了调试,谢谢!不过很好的信息
  • 我看不到你的图片。因为我办公室的一些限制。所以代码可能对理解很有帮助
  • 我已经提供了所有的代码...刚才,它没有出现?我想知道你为什么还在询问代码......
  • 你能提供代码吗,类似于你提供html的方式。由于我办公室的一些限制,我无法看到图像
  • 树视图的所有代码现已更新(触发填充树视图的按钮、HTML 代码和 SelectedNodeChanged 的​​事件处理程序)
猜你喜欢
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多