以编程方式将子节点添加到TreeView

时间:2015-07-25 17:39:02

标签: c# asp.net treeview

我在aspx页面中有这个代码:

<asp:TreeView ShowCheckBoxes="All" runat="server" ID="TreeView2">
<Nodes>
    <asp:TreeNode Text="Home" NavigateUrl="~/Home.aspx" Target="_blank" />
    <asp:TreeNode Text="Employee" Value="Employee" NavigateUrl="~/Employee.aspx" Target="_blank">
        <asp:TreeNode Text="UploadResume" Value="UploadResume"   NavigateUrl="~/Upload_Resume.aspx" Target="_blank" />
        <asp:TreeNode Text="Edit Resume" NavigateUrl="~/Edit_Resume.aspx" Target="_blank" />
        <asp:TreeNode Text="View Resume" NavigateUrl="~/View_Resume.aspx" Target="_blank" />
    </asp:TreeNode>
    <asp:TreeNode Text="Employer" NavigateUrl="~/Employer.aspx" Target="_blank">
        <asp:TreeNode Text="Upload Job" NavigateUrl="~/Upload_Job.aspx" Target="_blank" />
        <asp:TreeNode Text="Edit Job" NavigateUrl="~/Edit_Job.aspx" Target="_blank" />
        <asp:TreeNode Text="View Job" NavigateUrl="~/View_Job.aspx" Target="_blank" />
    </asp:TreeNode>
    <asp:TreeNode Text="Admin" NavigateUrl="~/Admin.aspx" Target="_blank">
        <asp:TreeNode Text="Add User" NavigateUrl="~/Add_User.aspx" Target="_blank" />
        <asp:TreeNode Text="Edit User" NavigateUrl="~/Edit_Use.aspx" Target="_blank" />
        <asp:TreeNode Text="View User" NavigateUrl="~/View_User.aspx" Target="_blank" />
    </asp:TreeNode>
</Nodes>

我想在UploadResume中将子节点添加到Page_Load。我使用此代码但运行时发生错误: Object reference not set to an instance of an object.

我该怎么做?

1 个答案:

答案 0 :(得分:2)

由于UploadResumeEmployee节点更改TreeView2.FindNode()的孩子,因为这样:

TreeView2.FindNode("Employee").ChildNodes[0].ChildNodes.Add(new TreeNode("majid", "majid"));